<?php

namespace Tests\Tenant;

use App\Filter\FilterIndex;
use App\VehicleType;
use Mtc\MercuryDataModels\Filters\MakeFilter;
use Mtc\MercuryDataModels\Filters\ModelFilter;
use Mtc\MercuryDataModels\Filters\VehicleTypeFilter;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;
use Tests\TenantTestCase;
use Tests\UserForTenant;

class FilterIndexTest extends TenantTestCase
{
    use UserForTenant;

    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testEscapesWhenNoId()
    {
        $model = new VehicleModel(['name' => 'foo']);
        FilterIndex::index('vehicle', $model, new ModelFilter());
        $this->assertEquals(0, FilterIndex::query()->count());
    }

    public function testEscapesWhenEmptySlug()
    {
        $model = VehicleModel::factory()->create(['name' => '']);
        FilterIndex::index('vehicle', $model, new ModelFilter());
        $this->assertEquals(0, FilterIndex::query()->count());
    }

    public function testIndexing()
    {
        $model = VehicleModel::factory()->create(['name' => 'foo']);
        FilterIndex::index('vehicle', $model, new ModelFilter());
        $this->assertEquals(1, FilterIndex::query()->count());
        $index = FilterIndex::query()->first();
        $this->assertEquals($model->id, $index->filter_id);
        $this->assertEquals('vehicle', $index->filter_type);

    }

    public function testExistingSlug()
    {
        $model = VehicleModel::factory()->create(['name' => 'foo']);
        FilterIndex::index('vehicle', $model, new ModelFilter());

        $model2 = VehicleModel::factory()->create(['name' => 'foo']);
        FilterIndex::index('vehicle', $model2, new ModelFilter());

        $this->assertEquals(2, FilterIndex::query()->count());
        $index = FilterIndex::query()->where('filter_id', $model2->id)->first();
        $this->assertEquals('foo-vehicle', $index->slug);

        $model3 = VehicleModel::factory()->create(['name' => 'foo']);
        FilterIndex::index('vehicle', $model3, new ModelFilter());

        $index = FilterIndex::query()->where('filter_id', $model3->id)->first();
        $this->assertEquals('foo-2', $index->slug);

    }

    public function testVehicleTypeUsesTypeValueNotVehicleSlug()
    {
        // Vehicle has its own URL slug ('audi-a3-1234') unrelated to its type ('car').
        // The vehicle_type index must record the type value, not the vehicle's slug.
        $vehicle = Vehicle::factory()->create([
            'slug' => 'audi-a3-1234',
            'type' => VehicleType::CAR->value,
        ]);

        FilterIndex::index('vehicle_type', $vehicle, new VehicleTypeFilter());

        $entry = FilterIndex::query()->where('filter_type', 'vehicle_type')->first();
        $this->assertNotNull($entry);
        $this->assertEquals(VehicleType::CAR->value, $entry->filter_id);
        $this->assertEquals('car', $entry->slug);
    }

    public function testVehicleTypeSlugNotSuffixedWhenConflictingWithModelSlug()
    {
        // A VehicleModel named "Car" lands in the index with slug 'car'.
        $model = VehicleModel::factory()->create(['name' => 'Car']);
        FilterIndex::index('model', $model, new ModelFilter());

        // Indexing vehicle_type 'car' must keep slug 'car' (not 'car-vehicle_type').
        $vehicle = Vehicle::factory()->create([
            'slug' => 'unrelated-vehicle-slug',
            'type' => VehicleType::CAR->value,
        ]);

        FilterIndex::index('vehicle_type', $vehicle, new VehicleTypeFilter());

        $entry = FilterIndex::query()->where('filter_type', 'vehicle_type')->first();
        $this->assertNotNull($entry);
        $this->assertEquals('car', $entry->slug);
    }

    public function testTaxonomyFilterPrefersStoredSlugColumn()
    {
        // Taxonomy filters (Make/Model) maintain their own slug column.
        // FilterIndex should reuse it instead of regenerating from name.
        $make = VehicleMake::factory()->create([
            'name' => 'Carco',
            'slug' => 'preset-make-slug',
        ]);

        FilterIndex::index('make', $make, new MakeFilter());

        $entry = FilterIndex::query()->where('filter_type', 'make')->first();
        $this->assertNotNull($entry);
        $this->assertEquals('preset-make-slug', $entry->slug);
    }
}
