<?php

namespace Tests\Tenant;

use App\Facades\Feature;
use App\Filter\FilterIndex;
use Illuminate\Support\Facades\Queue;
use Mtc\MercuryDataModels\BodyStyleType;
use Mtc\MercuryDataModels\FuelType;
use Mtc\MercuryDataModels\SearchFacet;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;
use Tests\FilterSettingSeed;
use Tests\TenantTestCase;

class SearchFacetsTest extends TenantTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        Queue::fake();
    }

    public function testFacetsReturnedInGetSiteWhenEnabled(): void
    {
        Feature::setEnabled(tenant(), ['search-facets']);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => false,
        ]);
        SearchFacet::create([
            'position' => 3,
            'filter_type' => 'body_type',
            'robots_index' => false,
            'robots_follow' => true,
        ]);

        $response = $this->getJson(route('get-site'));

        $response->assertStatus(200);
        $response->assertJsonCount(3, 'facets');
        $response->assertJsonFragment([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        $response->assertJsonFragment([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => false,
        ]);
        $response->assertJsonFragment([
            'position' => 3,
            'filter_type' => 'body_type',
            'robots_index' => false,
            'robots_follow' => true,
        ]);
    }

    public function testFacetsNotReturnedWhenFeatureDisabled(): void
    {
        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        $response = $this->getJson(route('get-site'));

        $response->assertStatus(200);
        $response->assertJson(['facets' => []]);
    }

    public function testFacetsReturnedInPositionOrder(): void
    {
        Feature::setEnabled(tenant(), ['search-facets']);

        SearchFacet::create([
            'position' => 3,
            'filter_type' => 'body_type',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        $response = $this->getJson(route('get-site'));

        $response->assertStatus(200);
        $facets = $response->json('facets');
        $this->assertEquals(1, $facets[0]['position']);
        $this->assertEquals('make', $facets[0]['filter_type']);
        $this->assertEquals(2, $facets[1]['position']);
        $this->assertEquals('model', $facets[1]['filter_type']);
        $this->assertEquals(3, $facets[2]['position']);
        $this->assertEquals('body_type', $facets[2]['filter_type']);
    }

    public function testFacetUrlDecodingMatchesSlugsToFilters(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $model = VehicleModel::factory()->create([
            'name' => 'Focus',
            'slug' => 'focus',
            'make_id' => $make->id,
        ]);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'ford',
            'name' => 'Ford',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'focus',
            'name' => 'Focus',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(3)->create([
            'is_published' => true,
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);
        Vehicle::factory(5)->create([
            'is_published' => true,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'ford/focus',
        ]);

        $response->assertStatus(200);
        $this->assertEquals(3, $response->json('results.total'));

        $selections = $response->json('selections');
        $this->assertNotEmpty($selections);
        $makeSelection = collect($selections)->firstWhere('type', 'make');
        $modelSelection = collect($selections)->firstWhere('type', 'model');
        $this->assertEquals('ford', $makeSelection['slug']);
        $this->assertEquals('focus', $modelSelection['slug']);
    }

    public function testFacetUrlDecodingAppliesFiltersCorrectly(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make1 = VehicleMake::factory()->create(['name' => 'BMW', 'slug' => 'bmw']);
        $make2 = VehicleMake::factory()->create(['name' => 'Audi', 'slug' => 'audi']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make1->id,
            'slug' => 'bmw',
            'name' => 'BMW',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make2->id,
            'slug' => 'audi',
            'name' => 'Audi',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(4)->create([
            'is_published' => true,
            'make_id' => $make1->id,
        ]);
        Vehicle::factory(6)->create([
            'is_published' => true,
            'make_id' => $make2->id,
        ]);

        $responseBmw = $this->postJson(route('vehicles.index'), [
            'slug' => 'bmw',
        ]);
        $responseBmw->assertStatus(200);
        $this->assertEquals(4, $responseBmw->json('results.total'));

        $responseAudi = $this->postJson(route('vehicles.index'), [
            'slug' => 'audi',
        ]);
        $responseAudi->assertStatus(200);
        $this->assertEquals(6, $responseAudi->json('results.total'));
    }

    public function testFacetOrderEnforcementRedirectsWrongOrder(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $model = VehicleModel::factory()->create([
            'name' => 'Fiesta',
            'slug' => 'fiesta',
            'make_id' => $make->id,
        ]);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'ford',
            'name' => 'Ford',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'fiesta',
            'name' => 'Fiesta',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'fiesta/ford',
            'base_url' => '/used-cars',
        ]);

        $response->assertStatus(404);
        $this->assertEquals('redirect', $response->json('type'));
        $this->assertStringContainsString('ford/fiesta', $response->json('to'));
    }

    public function testFacetOrderEnforcementNoRedirectForCorrectOrder(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $model = VehicleModel::factory()->create([
            'name' => 'Fiesta',
            'slug' => 'fiesta',
            'make_id' => $make->id,
        ]);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'ford',
            'name' => 'Ford',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'fiesta',
            'name' => 'Fiesta',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'ford/fiesta',
        ]);

        $response->assertStatus(200);
        $this->assertArrayNotHasKey('redirect', $response->json());
        $this->assertEquals(2, $response->json('results.total'));
    }

    public function testFacetOrderEnforcementWithSkippedPositions(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $bodyType = BodyStyleType::factory()->create(['name' => 'SUV', 'slug' => 'suv']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'toyota',
            'name' => 'Toyota',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'body_type',
            'filter_id' => $bodyType->id,
            'slug' => 'suv',
            'name' => 'SUV',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 3,
            'filter_type' => 'body_type',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'suv/toyota',
            'base_url' => '/used-cars',
        ]);

        $response->assertStatus(404);
        $this->assertEquals('redirect', $response->json('type'));
        $this->assertStringContainsString('toyota/suv', $response->json('to'));
    }

    public function testRobotsIndexFollowWhenFacetsDisabled(): void
    {
        $this->seed(FilterSettingSeed::class);

        $make = VehicleMake::factory()->create(['name' => 'Honda', 'slug' => 'honda']);
        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'honda',
            'name' => 'Honda',
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'honda',
        ]);

        $response->assertStatus(200);
        $seo = $response->json('seo');
        $this->assertTrue($seo['robots']['index']);
        $this->assertTrue($seo['robots']['follow']);
    }

    public function testRobotsBasedOnFacetSettingsPosition1(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Mazda', 'slug' => 'mazda']);
        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'mazda',
            'name' => 'Mazda',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => false,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'mazda',
        ]);

        $response->assertStatus(200);
        $seo = $response->json('seo');
        $this->assertFalse($seo['robots']['index']);
        $this->assertTrue($seo['robots']['follow']);
    }

    public function testRobotsBasedOnFacetSettingsPosition2(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Nissan', 'slug' => 'nissan']);
        $model = VehicleModel::factory()->create([
            'name' => 'Qashqai',
            'slug' => 'qashqai',
            'make_id' => $make->id,
        ]);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'nissan',
            'name' => 'Nissan',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'qashqai',
            'name' => 'Qashqai',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => false,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'nissan/qashqai',
        ]);

        $response->assertStatus(200);
        $seo = $response->json('seo');
        $this->assertTrue($seo['robots']['index']);
        $this->assertFalse($seo['robots']['follow']);
    }

    public function testRobotsUsesDeepestActiveFacetLevel(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Kia', 'slug' => 'kia']);
        $model = VehicleModel::factory()->create([
            'name' => 'Sportage',
            'slug' => 'sportage',
            'make_id' => $make->id,
        ]);
        $bodyType = BodyStyleType::factory()->create(['name' => 'Crossover', 'slug' => 'crossover']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'kia',
            'name' => 'Kia',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'sportage',
            'name' => 'Sportage',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'body_type',
            'filter_id' => $bodyType->id,
            'slug' => 'crossover',
            'name' => 'Crossover',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 3,
            'filter_type' => 'body_type',
            'robots_index' => false,
            'robots_follow' => false,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
            'model_id' => $model->id,
            'body_style_id' => $bodyType->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'kia/sportage/crossover',
        ]);

        $response->assertStatus(200);
        $seo = $response->json('seo');
        $this->assertFalse($seo['robots']['index']);
        $this->assertFalse($seo['robots']['follow']);
    }

    public function testRobotsDefaultsToIndexFollowWithNoSelections(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => false,
            'robots_follow' => false,
        ]);

        Vehicle::factory(2)->create(['is_published' => true]);

        $response = $this->postJson(route('vehicles.index'));

        $response->assertStatus(200);
        $seo = $response->json('seo');
        $this->assertTrue($seo['robots']['index']);
        $this->assertTrue($seo['robots']['follow']);
    }

    public function testPageUrlBuiltWithFacetPositionOrder(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Mercedes', 'slug' => 'mercedes']);
        $model = VehicleModel::factory()->create([
            'name' => 'C-Class',
            'slug' => 'c-class',
            'make_id' => $make->id,
        ]);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'mercedes',
            'name' => 'Mercedes',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'c-class',
            'name' => 'C-Class',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'selections' => [
                ['type' => 'make', 'value' => 'mercedes'],
                ['type' => 'model', 'value' => 'c-class'],
            ],
        ]);

        $response->assertStatus(200);
        $url = $response->json('url');
        $this->assertEquals('mercedes/c-class', $url);
    }

    public function testMultipleValuesForFacetMovedToQueryParams(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make1 = VehicleMake::factory()->create(['name' => 'BMW', 'slug' => 'bmw']);
        $make2 = VehicleMake::factory()->create(['name' => 'Audi', 'slug' => 'audi']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make1->id,
            'slug' => 'bmw',
            'name' => 'BMW',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make2->id,
            'slug' => 'audi',
            'name' => 'Audi',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make1->id,
        ]);
        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make2->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'selections' => [
                ['type' => 'make', 'value' => 'bmw'],
                ['type' => 'make', 'value' => 'audi'],
            ],
        ]);

        $response->assertStatus(200);
        $url = $response->json('url');

        // Multiple values for a facet type should be in query params, not path
        $this->assertStringContainsString('?', $url);
        $this->assertStringContainsString('make', $url);
        // Path should be empty (no facet slugs)
        $urlParts = explode('?', $url);
        $this->assertEmpty($urlParts[0], 'URL path should be empty when multiple values for single facet type');
    }

    public function testMultipleFacetValuesViaUrlSlugRedirectsToQueryParams(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make1 = VehicleMake::factory()->create(['name' => 'BMW', 'slug' => 'bmw']);
        $make2 = VehicleMake::factory()->create(['name' => 'Audi', 'slug' => 'audi']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make1->id,
            'slug' => 'bmw',
            'name' => 'BMW',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make2->id,
            'slug' => 'audi',
            'name' => 'Audi',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make1->id,
        ]);
        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make2->id,
        ]);

        // User tries to access URL with two makes in path - this should trigger redirect
        // because multiple values for the same facet type should be in query params
        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'bmw/audi',
            'base_url' => '/used-cars',
        ]);

        $response->assertStatus(404);

        // When multiple values exist for a single facet type in the URL path,
        // the system should redirect to use query params for the second value
        $this->assertEquals('redirect', $response->json('type'), 'Should redirect when multiple values for same facet type in path');
        $redirect = $response->json('to');
        $this->assertStringContainsString('bmw', $redirect);
        $this->assertStringContainsString('make', $redirect);
    }

    public function testNonFacetFiltersInQueryParams(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Volvo', 'slug' => 'volvo']);
        $bodyType = BodyStyleType::factory()->create(['name' => 'Estate', 'slug' => 'estate']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'volvo',
            'name' => 'Volvo',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'body_type',
            'filter_id' => $bodyType->id,
            'slug' => 'estate',
            'name' => 'Estate',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'make_id' => $make->id,
            'body_style_id' => $bodyType->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'selections' => [
                ['type' => 'make', 'value' => 'volvo'],
                ['type' => 'body_type', 'value' => 'estate'],
            ],
        ]);

        $response->assertStatus(200);
        $url = $response->json('url');
        $this->assertStringStartsWith('volvo', $url);
        $this->assertStringContainsString('body_type', $url);
    }

    public function testNonFacetSelectionsInSlugTriggerRedirect(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $make = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $model = VehicleModel::factory()->create([
            'name' => 'Fiesta',
            'slug' => 'fiesta',
            'make_id' => $make->id,
        ]);
        $bodyType = BodyStyleType::factory()->create(['name' => 'Hatchback', 'slug' => 'hatchback']);

        FilterIndex::factory()->create([
            'filter_type' => 'make',
            'filter_id' => $make->id,
            'slug' => 'ford',
            'name' => 'Ford',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'fiesta',
            'name' => 'Fiesta',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'body_type',
            'filter_id' => $bodyType->id,
            'slug' => 'hatchback',
            'name' => 'Hatchback',
        ]);

        // Only make and model are configured as facets
        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'make',
            'robots_index' => true,
            'robots_follow' => true,
        ]);
        SearchFacet::create([
            'position' => 2,
            'filter_type' => 'model',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        // body_type is NOT a configured facet but is in the slug path
        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'ford/hatchback/fiesta',
            'base_url' => '/used-cars',
        ]);

        $response->assertStatus(404);
        $this->assertEquals('redirect', $response->json('type'));
        $redirect = $response->json('to');
        $this->assertStringContainsString('ford/fiesta', $redirect);
        $this->assertStringContainsString('body_type', $redirect);
        $this->assertStringContainsString('hatchback', $redirect);
    }

    public function testDuplicateSlugAcrossFilterTypesDoesNotCauseRedirectLoop(): void
    {
        $this->seed(FilterSettingSeed::class);
        Feature::setEnabled(tenant(), ['search-facets']);

        $fuelType = FuelType::factory()->create(['name' => 'Electric', 'slug' => 'electric']);
        $model = VehicleModel::factory()->create(['name' => 'Electric', 'slug' => 'electric']);

        // Same slug "electric" exists under two different filter types
        FilterIndex::factory()->create([
            'filter_type' => 'fuel_type',
            'filter_id' => $fuelType->id,
            'slug' => 'electric',
            'name' => 'Electric',
        ]);
        FilterIndex::factory()->create([
            'filter_type' => 'model',
            'filter_id' => $model->id,
            'slug' => 'electric',
            'name' => 'Electric',
        ]);

        SearchFacet::create([
            'position' => 1,
            'filter_type' => 'fuel_type',
            'robots_index' => true,
            'robots_follow' => true,
        ]);

        Vehicle::factory(2)->create([
            'is_published' => true,
            'fuel_type_id' => $fuelType->id,
        ]);

        $response = $this->postJson(route('vehicles.index'), [
            'slug' => 'electric',
            'base_url' => '/used-cars',
        ]);

        // Should NOT redirect to /used-cars/electric/electric
        $response->assertStatus(200);

        $selections = collect($response->json('selections'));
        $fuelSelection = $selections->firstWhere('type', 'fuel_type');
        $this->assertNotNull($fuelSelection);
        $this->assertEquals('electric', $fuelSelection['value']);

        // Should NOT have matched to model as well
        $modelSelection = $selections->firstWhere('type', 'model');
        $this->assertNull($modelSelection);
    }
}
