<?php

namespace Tests\Feature;

use App\Models\Park;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ParkControllerTest extends TestCase
{
    use RefreshDatabase;

    protected User $user;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user = User::factory()->create();
    }

    /** @test */
    public function it_can_display_parks_index(): void
    {
        Park::factory()->count(3)->create();

        $response = $this->actingAs($this->user)->get(route('parks.index'));

        $response->assertStatus(200)
                ->assertSee('Parks');
    }

    /** @test */
    public function it_can_create_a_park(): void
    {
        $parkData = [
            'name' => 'Test Park',
            'description' => 'Test description',
            'is_active' => true,
        ];

        $response = $this->actingAs($this->user)->post(route('parks.store'), $parkData);

        $response->assertRedirect();
        $this->assertDatabaseHas('parks', $parkData);
    }

    /** @test */
    public function it_can_show_a_park(): void
    {
        $park = Park::factory()->create();

        $response = $this->actingAs($this->user)->get(route('parks.show', $park));

        $response->assertStatus(200)
                ->assertSee($park->name);
    }

    /** @test */
    public function it_can_update_a_park(): void
    {
        $park = Park::factory()->create();
        $updateData = [
            'name' => 'Updated Park',
            'description' => 'Updated description',
            'is_active' => false,
        ];

        $response = $this->actingAs($this->user)->put(route('parks.update', $park), $updateData);

        $response->assertRedirect();
        $this->assertDatabaseHas('parks', $updateData);
    }

    /** @test */
    public function it_can_delete_a_park(): void
    {
        $park = Park::factory()->create();

        $response = $this->actingAs($this->user)->delete(route('parks.destroy', $park));

        $response->assertRedirect();
        $this->assertSoftDeleted('parks', ['id' => $park->id]);
    }

    /** @test */
    public function it_validates_required_fields(): void
    {
        $response = $this->actingAs($this->user)->post(route('parks.store'), []);

        $response->assertSessionHasErrors(['name']);
    }

    /** @test */
    public function it_can_search_parks(): void
    {
        $park1 = Park::factory()->create(['name' => 'Searchable Park']);
        $park2 = Park::factory()->create(['name' => 'Other Park']);

        $response = $this->actingAs($this->user)->get(route('parks.index', ['search' => 'Searchable']));

        $response->assertStatus(200)
                ->assertSee($park1->name)
                ->assertDontSee($park2->name);
    }
}