<?php

namespace Tests\Feature;

use App\Models\Manufacturer;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

/**
 * Manufacturer Model Feature Tests
 * 
 * Tests for ThrillWiki Manufacturer model functionality
 */
class ManufacturerTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    /**
     * Test model creation.
     */
    public function test_can_create_manufacturer(): void
    {
        $manufacturer = Manufacturer::factory()->create();

        $this->assertDatabaseHas('manufacturers', [
            'id' => $manufacturer->id,
            'name' => $manufacturer->name,
        ]);
    }

    /**
     * Test model factory.
     */
    public function test_manufacturer_factory_works(): void
    {
        $manufacturer = Manufacturer::factory()->create();

        $this->assertInstanceOf(Manufacturer::class, $manufacturer);
        $this->assertNotEmpty($manufacturer->name);
        $this->assertIsBool($manufacturer->is_active);
    }

    /**
     * Test active scope.
     */
    public function test_active_scope_filters_correctly(): void
    {
        Manufacturer::factory()->active()->create();
        Manufacturer::factory()->inactive()->create();

        $activeCount = Manufacturer::active()->count();
        $totalCount = Manufacturer::count();

        $this->assertEquals(1, $activeCount);
        $this->assertEquals(2, $totalCount);
    }

    /**
     * Test major manufacturers scope.
     */
    public function test_major_manufacturers_scope(): void
    {
        // Create manufacturers with different ride counts
        Manufacturer::factory()->create(['total_rides' => 10]);
        Manufacturer::factory()->create(['total_rides' => 3]);
        Manufacturer::factory()->create(['total_rides' => 8]);

        $majorManufacturers = Manufacturer::majorManufacturers()->get();
        $this->assertEquals(2, $majorManufacturers->count());

        // Test custom minimum
        $majorManufacturers = Manufacturer::majorManufacturers(8)->get();
        $this->assertEquals(2, $majorManufacturers->count());
    }

    /**
     * Test coaster manufacturers scope.
     */
    public function test_coaster_manufacturers_scope(): void
    {
        Manufacturer::factory()->create(['total_roller_coasters' => 5]);
        Manufacturer::factory()->create(['total_roller_coasters' => 0]);
        
        $coasterManufacturers = Manufacturer::coasterManufacturers()->get();
        $this->assertEquals(1, $coasterManufacturers->count());
    }

    /**
     * Test optimized scope.
     */
    public function test_optimized_scope(): void
    {
        $manufacturer = Manufacturer::factory()->create();
        
        $optimized = Manufacturer::optimized()->first();
        $this->assertInstanceOf(Manufacturer::class, $optimized);
        $this->assertEquals($manufacturer->id, $optimized->id);
    }

    /**
     * Test display name attribute.
     */
    public function test_display_name_attribute(): void
    {
        $manufacturer = Manufacturer::factory()->create([
            'name' => 'Intamin AG',
            'total_rides' => 15
        ]);

        $this->assertEquals('Intamin AG (15 rides)', $manufacturer->display_name);
    }

    /**
     * Test website URL attribute.
     */
    public function test_website_url_attribute(): void
    {
        // Test with https URL
        $manufacturer1 = Manufacturer::factory()->create(['website' => 'https://intamin.com']);
        $this->assertEquals('https://intamin.com', $manufacturer1->website_url);

        // Test with http URL
        $manufacturer2 = Manufacturer::factory()->create(['website' => 'http://intamin.com']);
        $this->assertEquals('http://intamin.com', $manufacturer2->website_url);

        // Test without protocol
        $manufacturer3 = Manufacturer::factory()->create(['website' => 'intamin.com']);
        $this->assertEquals('https://intamin.com', $manufacturer3->website_url);

        // Test empty website
        $manufacturer4 = Manufacturer::factory()->create(['website' => null]);
        $this->assertEquals('', $manufacturer4->website_url);
    }

    /**
     * Test update statistics method.
     */
    public function test_update_statistics(): void
    {
        $manufacturer = Manufacturer::factory()->create([
            'total_rides' => 0,
            'total_roller_coasters' => 0
        ]);

        // This test assumes Ride model exists and has proper relationship
        // For now, we'll just test that the method exists and doesn't error
        $manufacturer->updateStatistics();
        
        // Verify the method completes without error
        $this->assertTrue(true);
    }

    /**
     * Test route key name.
     */
    public function test_route_key_name(): void
    {
        $manufacturer = new Manufacturer();
        $this->assertEquals('slug', $manufacturer->getRouteKeyName());
    }

    /**
     * Test soft deletes.
     */
    public function test_soft_deletes_work(): void
    {
        $manufacturer = Manufacturer::factory()->create();
        $manufacturer->delete();

        $this->assertSoftDeleted($manufacturer);
        
        // Test that it's excluded from normal queries
        $this->assertEquals(0, Manufacturer::count());
        
        // Test that it's included in withTrashed queries
        $this->assertEquals(1, Manufacturer::withTrashed()->count());
    }
}