<?php

use App\Livewire\ParkListComponent;
use App\Models\Park;
use App\Models\Operator;
use App\Enums\ParkStatus;
use Livewire\Livewire;
use function Pest\Laravel\get;

test('parks page loads successfully', function () {
    get('/parks')->assertSuccessful();
});

test('park list component shows parks', function () {
    $park = Park::factory()->create([
        'name' => 'Test Park',
        'status' => ParkStatus::OPERATING
    ]);

    Livewire::test(ParkListComponent::class)
        ->assertSee('Test Park');
});

test('search filters parks', function () {
    $matchingPark = Park::factory()->create(['name' => 'Magic Kingdom']);
    $nonMatchingPark = Park::factory()->create(['name' => 'Epcot']);

    Livewire::test(ParkListComponent::class)
        ->set('search', 'Magic')
        ->assertSee('Magic Kingdom')
        ->assertDontSee('Epcot');
});

test('status filter shows only matching parks', function () {
    $operatingPark = Park::factory()->create([
        'name' => 'Operating Park',
        'status' => ParkStatus::OPERATING
    ]);
    
    $closedPark = Park::factory()->create([
        'name' => 'Closed Park',
        'status' => ParkStatus::CLOSED_PERM
    ]);

    Livewire::test(ParkListComponent::class)
        ->set('status', ParkStatus::OPERATING->value)
        ->assertSee('Operating Park')
        ->assertDontSee('Closed Park');
});

test('operator filter shows only matching parks', function () {
    $operator1 = Operator::factory()->create(['name' => 'Disney']);
    $operator2 = Operator::factory()->create(['name' => 'Universal']);
    
    $disneyPark = Park::factory()->create([
        'name' => 'Disney Park',
        'operator_id' => $operator1->id
    ]);
    
    $universalPark = Park::factory()->create([
        'name' => 'Universal Park',
        'operator_id' => $operator2->id
    ]);

    Livewire::test(ParkListComponent::class)
        ->set('operator', $operator1->id)
        ->assertSee('Disney Park')
        ->assertDontSee('Universal Park');
});

test('sorting works correctly', function () {
    $laterPark = Park::factory()->create([
        'name' => 'Later Park',
        'opening_date' => '2020-01-01'
    ]);
    
    $earlierPark = Park::factory()->create([
        'name' => 'Earlier Park',
        'opening_date' => '2010-01-01'
    ]);

    // Test date sorting ascending
    Livewire::test(ParkListComponent::class)
        ->set('sort', 'opening_date')
        ->set('direction', 'asc')
        ->assertSeeInOrder(['Earlier Park', 'Later Park']);

    // Test date sorting descending
    Livewire::test(ParkListComponent::class)
        ->set('sort', 'opening_date')
        ->set('direction', 'desc')
        ->assertSeeInOrder(['Later Park', 'Earlier Park']);
});

test('pagination works', function () {
    // Create 15 parks (more than the per-page limit of 12)
    Park::factory()->count(15)->create();

    Livewire::test(ParkListComponent::class)
        ->assertViewHas('parks', function ($parks) {
            return $parks->count() == 12; // First page should have 12 items
        });
});

test('query string parameters work', function () {
    $park = Park::factory()->create([
        'name' => 'Test Park',
        'status' => ParkStatus::OPERATING
    ]);

    get('/parks?search=Test&status=' . ParkStatus::OPERATING->value)
        ->assertSuccessful()
        ->assertSee('Test Park');
});