<?php

namespace Tests\Unit;

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

class ParkModelTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function it_can_add_photos()
    {
        $park = Park::factory()->create();
        
        $photo = $park->addPhoto([
            'title' => 'Test Photo',
            'file_path' => 'parks/test.jpg',
            'file_name' => 'test.jpg',
        ]);
        
        $this->assertInstanceOf(Photo::class, $photo);
        $this->assertEquals('Test Photo', $photo->title);
        $this->assertEquals(1, $park->photos()->count());
        $this->assertTrue($photo->is_featured);
    }
    
    /** @test */
    public function it_can_set_featured_photo()
    {
        $park = Park::factory()->create();
        
        $photo1 = $park->addPhoto([
            'title' => 'Photo 1',
            'file_path' => 'parks/photo1.jpg',
            'file_name' => 'photo1.jpg',
        ]);
        
        $photo2 = $park->addPhoto([
            'title' => 'Photo 2',
            'file_path' => 'parks/photo2.jpg',
            'file_name' => 'photo2.jpg',
        ]);
        
        $this->assertTrue($photo1->is_featured);
        $this->assertFalse($photo2->is_featured);
        
        $park->setFeaturedPhoto($photo2);
        
        $photo1->refresh();
        $photo2->refresh();
        
        $this->assertFalse($photo1->is_featured);
        $this->assertTrue($photo2->is_featured);
    }
    
    /** @test */
    public function it_can_reorder_photos()
    {
        $park = Park::factory()->create();
        
        $photo1 = $park->addPhoto([
            'title' => 'Photo 1',
            'file_path' => 'parks/photo1.jpg',
            'file_name' => 'photo1.jpg',
            'position' => 1,
        ]);
        
        $photo2 = $park->addPhoto([
            'title' => 'Photo 2',
            'file_path' => 'parks/photo2.jpg',
            'file_name' => 'photo2.jpg',
            'position' => 2,
        ]);
        
        $photo3 = $park->addPhoto([
            'title' => 'Photo 3',
            'file_path' => 'parks/photo3.jpg',
            'file_name' => 'photo3.jpg',
            'position' => 3,
        ]);
        
        // Reorder: 3, 1, 2
        $park->reorderPhotos([$photo3->id, $photo1->id, $photo2->id]);
        
        $photo1->refresh();
        $photo2->refresh();
        $photo3->refresh();
        
        $this->assertEquals(1, $photo3->position);
        $this->assertEquals(2, $photo1->position);
        $this->assertEquals(3, $photo2->position);
    }
    
    /** @test */
    public function it_can_get_by_slug()
    {
        $park = Park::factory()->create([
            'name' => 'Test Park',
            'slug' => 'test-park',
        ]);
        
        // Test current slug
        [$foundPark, $isHistorical] = Park::getBySlug('test-park');
        
        $this->assertNotNull($foundPark);
        $this->assertEquals($park->id, $foundPark->id);
        $this->assertFalse($isHistorical);
        
        // Change slug and test historical slug
        $park->slug = 'new-test-park';
        $park->save();
        
        [$foundPark, $isHistorical] = Park::getBySlug('test-park');
        
        $this->assertNotNull($foundPark);
        $this->assertEquals($park->id, $foundPark->id);
        $this->assertTrue($isHistorical);
    }
    
    /** @test */
    public function it_returns_absolute_url()
    {
        $park = Park::factory()->create([
            'slug' => 'test-park',
        ]);
        
        $this->assertEquals(route('parks.show', ['slug' => 'test-park']), $park->getAbsoluteUrl());
    }
    
    /** @test */
    public function it_returns_formatted_location()
    {
        $park = Park::factory()->create();
        
        // Without location
        $this->assertEquals('', $park->formatted_location);
        
        // With location
        $location = $park->updateLocation([
            'address' => '123 Main St',
            'city' => 'Orlando',
            'state' => 'FL',
            'country' => 'USA',
        ]);
        
        $this->assertEquals('123 Main St, Orlando, FL, USA', $location->formatted_address);
    }
}