<?php

use App\Filament\Resources\WeeklyForecasts\Pages\ListWeeklyForecasts;
use App\Filament\Resources\WeeklyForecasts\Pages\ViewWeeklyForecast;
use App\Filament\Resources\WeeklyForecasts\WeeklyForecastResource;
use App\Models\User;
use App\Models\WeeklyForecast;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function () {
    $this->admin = User::factory()->admin()->create();
    $this->actingAs($this->admin);
});

it('lists weekly forecasts sorted by forecast_for desc', function () {
    $older = WeeklyForecast::factory()->create(['forecast_for' => '2026-04-06']);
    $newer = WeeklyForecast::factory()->create(['forecast_for' => '2026-05-04']);

    Livewire::test(ListWeeklyForecasts::class)
        ->assertOk()
        ->assertCanSeeTableRecords([$newer, $older], inOrder: true);
});

it('disables create and edit', function () {
    $forecast = WeeklyForecast::factory()->create();

    expect(WeeklyForecastResource::canCreate())->toBeFalse();
    expect(WeeklyForecastResource::canEdit($forecast))->toBeFalse();
    expect(WeeklyForecastResource::canDelete($forecast))->toBeFalse();
});

it('renders the view page for a forecast', function () {
    $forecast = WeeklyForecast::factory()->create([
        'reasoning' => 'Brent stabilising; supermarket cycle entering bottom.',
    ]);

    Livewire::test(ViewWeeklyForecast::class, ['record' => $forecast->id])
        ->assertOk()
        ->assertSee('Brent stabilising; supermarket cycle entering bottom.');
});
