<?php

namespace Tests\Tenant;

use Mtc\MercuryDataModels\Media;
use Mtc\MercuryDataModels\MediaUse;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;
use Mtc\MercuryDataModels\VehicleReview;
use Tests\TenantTestCase;

class VehicleContentControllerTest extends TenantTestCase
{
    public function testVideosEndpointReturnsExpectedStructure(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

        Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $response->assertJsonStructure([
            'data' => [
                '*' => ['slug', 'title', 'thumbnail', 'video_url']
            ],
            'pagination' => [
                'current_page',
                'last_page',
                'per_page',
                'total',
                'first_page_url',
                'last_page_url',
                'next_page_url',
                'prev_page_url',
            ],
        ]);
        $this->assertStringStartsWith('/vehicles/', $response->json('data.0.slug'));
        $this->assertEquals(15, $response->json('pagination.per_page'));
    }

    public function testVideosEndpointFiltersByMakeAndModel(): void
    {
        $make1 = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $make2 = VehicleMake::factory()->create(['name' => 'Honda', 'slug' => 'honda']);
        $model1 = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make1->id,
        ]);

        Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/toyota.mp4',
            'make_id' => $make1->id,
            'model_id' => $model1->id,
        ]);

        Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/honda.mp4',
            'make_id' => $make2->id,
        ]);

        $response = $this->getJson(route('vehicle-content.videos', [
            'make' => 'toyota',
            'model' => 'corolla',
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testVideosEndpointExcludesVehiclesWithoutVideo(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);

        Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
        ]);

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

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

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testVideosEndpointGroupsByMakeModel(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

        Vehicle::factory(3)->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testVideosEndpointReturnsEmptyForNoResults(): void
    {
        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertEmpty($response->json('data'));
    }

    public function testReviewsEndpointRequiresProvider(): void
    {
        $response = $this->getJson(route('vehicle-content.reviews'));

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['provider']);
    }

    public function testReviewsEndpointReturnsExpectedStructure(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

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

        $review = VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'review' => [
                ['type' => 'Ten Second Review', 'headline' => 'Test', 'text' => 'Review text'],
            ],
        ]);

        $vehicle->reviews()->attach($review->id);

        $response = $this->getJson(route('vehicle-content.reviews', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $response->assertJsonStructure([
            'data' => [
                '*' => ['slug', 'title', 'thumbnail', 'review']
            ],
            'pagination' => [
                'current_page',
                'last_page',
                'per_page',
                'total',
                'first_page_url',
                'last_page_url',
                'next_page_url',
                'prev_page_url',
            ],
        ]);
        $this->assertStringStartsWith('/vehicles/', $response->json('data.0.slug'));
    }

    public function testReviewsEndpointFiltersByProvider(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);

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

        $cadReview = VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'review' => [['type' => 'Summary', 'text' => 'CAD review']],
        ]);

        $otherReview = VehicleReview::factory()->create([
            'provider' => 'other-provider',
            'review' => [['type' => 'Summary', 'text' => 'Other review']],
        ]);

        $vehicle->reviews()->attach([$cadReview->id, $otherReview->id]);

        $response = $this->getJson(route('vehicle-content.reviews', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testReviewsEndpointFiltersByMakeAndModel(): void
    {
        $make1 = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $make2 = VehicleMake::factory()->create(['name' => 'Honda', 'slug' => 'honda']);
        $model1 = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make1->id,
        ]);

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

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

        $review1 = VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make' => 'Toyota',
            'model' => 'Corolla',
            'review' => [['type' => 'Summary', 'text' => 'Toyota review']],
        ]);

        $review2 = VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make' => 'Honda',
            'review' => [['type' => 'Summary', 'text' => 'Honda review']],
        ]);

        $vehicle1->reviews()->attach($review1->id);
        $vehicle2->reviews()->attach($review2->id);

        $response = $this->getJson(route('vehicle-content.reviews', [
            'provider' => 'car-and-driving',
            'make' => 'toyota',
            'model' => 'corolla',
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testReviewsEndpointReturnsEmptyForNoResults(): void
    {
        $response = $this->getJson(route('vehicle-content.reviews', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertEmpty($response->json('data'));
    }

    public function testVideosEndpointPaginatesResults(): void
    {
        for ($i = 1; $i <= 5; $i++) {
            $make = VehicleMake::factory()->create(['name' => "Make{$i}", 'slug' => "make{$i}"]);
            $model = VehicleModel::factory()->create([
                'name' => "Model{$i}",
                'slug' => "model{$i}",
                'make_id' => $make->id,
            ]);
            Vehicle::factory()->create([
                'is_published' => true,
                'main_video_url' => "https://example.com/video{$i}.mp4",
                'make_id' => $make->id,
                'model_id' => $model->id,
            ]);
        }

        $response = $this->getJson(route('vehicle-content.videos', ['per_page' => 2, 'page' => 1]));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json('data'));
        $this->assertEquals(1, $response->json('pagination.current_page'));
        $this->assertEquals(3, $response->json('pagination.last_page'));
        $this->assertEquals(2, $response->json('pagination.per_page'));
        $this->assertEquals(5, $response->json('pagination.total'));

        $response2 = $this->getJson(route('vehicle-content.videos', ['per_page' => 2, 'page' => 2]));

        $response2->assertStatus(200);
        $this->assertCount(2, $response2->json('data'));
        $this->assertEquals(2, $response2->json('pagination.current_page'));

        $response3 = $this->getJson(route('vehicle-content.videos', ['per_page' => 2, 'page' => 3]));

        $response3->assertStatus(200);
        $this->assertCount(1, $response3->json('data'));
        $this->assertEquals(3, $response3->json('pagination.current_page'));
    }

    public function testPaginationValidatesPerPageMax(): void
    {
        $response = $this->getJson(route('vehicle-content.videos', ['per_page' => 150]));

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['per_page']);
    }

    public function testVideosEndpointNullsThumbnailForSingleImageWhenFlagEnabled(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

        $vehicle = Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $media = Media::factory()->create();
        MediaUse::factory()->create([
            'media_id' => $media->id,
            'owner_type' => 'vehicle',
            'owner_id' => $vehicle->id,
            'primary' => true,
        ]);

        $response = $this->getJson(route('vehicle-content.videos', [
            'single_image_placeholder' => true,
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertNull($response->json('data.0.thumbnail'));
    }

    public function testVideosEndpointKeepsThumbnailForMultipleImagesWhenFlagEnabled(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

        $vehicle = Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $media1 = Media::factory()->create();
        $media2 = Media::factory()->create();
        MediaUse::factory()->create([
            'media_id' => $media1->id,
            'owner_type' => 'vehicle',
            'owner_id' => $vehicle->id,
            'primary' => true,
        ]);
        MediaUse::factory()->create([
            'media_id' => $media2->id,
            'owner_type' => 'vehicle',
            'owner_id' => $vehicle->id,
            'primary' => false,
        ]);

        $response = $this->getJson(route('vehicle-content.videos', [
            'single_image_placeholder' => true,
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertNotNull($response->json('data.0.thumbnail'));
    }

    public function testVideosEndpointKeepsThumbnailForSingleImageWhenFlagDisabled(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

        $vehicle = Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $media = Media::factory()->create();
        MediaUse::factory()->create([
            'media_id' => $media->id,
            'owner_type' => 'vehicle',
            'owner_id' => $vehicle->id,
            'primary' => true,
        ]);

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertNotNull($response->json('data.0.thumbnail'));
    }

    public function testVideosEndpointResolvesCorrectModelWhenDuplicateSlugsExist(): void
    {
        $fordMake = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $otherMake = VehicleMake::factory()->create(['name' => 'OtherBrand', 'slug' => 'otherbrand']);

        $fordRanger = VehicleModel::factory()->create([
            'name' => 'Ranger',
            'slug' => 'ranger',
            'make_id' => $fordMake->id,
        ]);

        $otherRanger = VehicleModel::factory()->create([
            'name' => 'Ranger',
            'slug' => 'ranger',
            'make_id' => $otherMake->id,
        ]);

        Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/ford-ranger.mp4',
            'make_id' => $fordMake->id,
            'model_id' => $fordRanger->id,
        ]);

        $response = $this->getJson(route('vehicle-content.videos', [
            'make' => 'ford',
            'model' => 'ranger',
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testReviewsEndpointResolvesCorrectModelWhenDuplicateSlugsExist(): void
    {
        $fordMake = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $otherMake = VehicleMake::factory()->create(['name' => 'OtherBrand', 'slug' => 'otherbrand']);

        $fordRanger = VehicleModel::factory()->create([
            'name' => 'Ranger',
            'slug' => 'ranger',
            'make_id' => $fordMake->id,
        ]);

        $otherRanger = VehicleModel::factory()->create([
            'name' => 'Ranger',
            'slug' => 'ranger',
            'make_id' => $otherMake->id,
        ]);

        $vehicle = Vehicle::factory()->create([
            'is_published' => true,
            'make_id' => $fordMake->id,
            'model_id' => $fordRanger->id,
        ]);

        $review = VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make' => 'Ford',
            'model' => 'Ranger',
            'review' => [['type' => 'Summary', 'text' => 'Ford Ranger review']],
        ]);

        $vehicle->reviews()->attach($review->id);

        $response = $this->getJson(route('vehicle-content.reviews', [
            'provider' => 'car-and-driving',
            'make' => 'ford',
            'model' => 'ranger',
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
    }

    public function testVideosEndpointNullsThumbnailForZeroImagesWhenFlagEnabled(): void
    {
        $make = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create([
            'name' => 'Corolla',
            'slug' => 'corolla',
            'make_id' => $make->id,
        ]);

        Vehicle::factory()->create([
            'is_published' => true,
            'main_video_url' => 'https://example.com/video.mp4',
            'make_id' => $make->id,
            'model_id' => $model->id,
        ]);

        $response = $this->getJson(route('vehicle-content.videos', [
            'single_image_placeholder' => true,
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertNull($response->json('data.0.thumbnail'));
    }

    // -------------------------------------------------------------------------
    // Non-stock reviews
    // -------------------------------------------------------------------------

    public function testReviewsEndpointReturnsNonStockReviewWithNullSlug(): void
    {
        VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'Ford',
            'model'    => 'Ranger',
            'review'   => [['type' => 'Summary', 'text' => 'Non-stock Ford Ranger review']],
        ]);

        $response = $this->getJson(route('vehicle-content.reviews', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertNull($response->json('data.0.slug'));
    }

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

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

        $stockReview = VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'Ford',
            'model'    => 'Focus',
            'review'   => [['type' => 'Summary', 'text' => 'Stock Ford Focus review']],
        ]);
        $vehicle->reviews()->attach($stockReview->id);

        VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'Ford',
            'model'    => 'Ranger',
            'review'   => [['type' => 'Summary', 'text' => 'Non-stock Ford Ranger review']],
        ]);

        $response = $this->getJson(route('vehicle-content.reviews', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json('data'));
    }

    public function testReviewsEndpointPlacesFranchiseMakesFirst(): void
    {
        // Ford is franchise (has active stock)
        $fordMake  = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $fordModel = VehicleModel::factory()->create(['name' => 'Focus', 'slug' => 'focus', 'make_id' => $fordMake->id]);
        Vehicle::factory()->create(['is_published' => true, 'make_id' => $fordMake->id, 'model_id' => $fordModel->id]);

        // Non-stock franchise review
        VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'Ford',
            'model'    => 'Ranger',
            'review'   => [['type' => 'Summary', 'text' => 'Ford Ranger review']],
        ]);

        // Non-stock non-franchise review
        VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'Tesla',
            'model'    => 'Model 3',
            'review'   => [['type' => 'Summary', 'text' => 'Tesla Model 3 review']],
        ]);

        $response = $this->getJson(route('vehicle-content.reviews', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json('data'));
        $this->assertStringContainsString('Ford', $response->json('data.0.title'));
    }

    public function testReviewsEndpointFiltersByMakeForNonStockContent(): void
    {
        VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'Ford',
            'model'    => 'Ranger',
            'review'   => [['type' => 'Summary', 'text' => 'Ford Ranger review']],
        ]);

        VehicleReview::factory()->create([
            'provider' => 'car-and-driving',
            'make'     => 'BMW',
            'model'    => '3 Series',
            'review'   => [['type' => 'Summary', 'text' => 'BMW review']],
        ]);

        // Seed a Ford make so slug resolves
        $fordMake = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);

        $response = $this->getJson(route('vehicle-content.reviews', [
            'provider' => 'car-and-driving',
            'make'     => 'ford',
        ]));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertStringContainsString('Ford', $response->json('data.0.title'));
    }

    // -------------------------------------------------------------------------
    // Non-stock videos
    // -------------------------------------------------------------------------

    public function testVideosEndpointReturnsNonStockCdVideoWithNullSlug(): void
    {
        VehicleReview::factory()->create([
            'provider'       => 'car-and-driving',
            'make'           => 'Ford',
            'model'          => 'Ranger',
            'main_video_url' => 'https://cdn.caranddriving.com/ford-ranger.mp4',
        ]);

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertNull($response->json('data.0.slug'));
        $this->assertEquals('https://cdn.caranddriving.com/ford-ranger.mp4', $response->json('data.0.video_url'));
    }

    public function testVideosEndpointReturnsBothStockAndNonStockVideos(): void
    {
        $make  = VehicleMake::factory()->create(['name' => 'Toyota', 'slug' => 'toyota']);
        $model = VehicleModel::factory()->create(['name' => 'Corolla', 'slug' => 'corolla', 'make_id' => $make->id]);

        Vehicle::factory()->create([
            'is_published'   => true,
            'main_video_url' => 'https://example.com/corolla.mp4',
            'make_id'        => $make->id,
            'model_id'       => $model->id,
        ]);

        // Non-stock C&D video (no linked vehicle)
        VehicleReview::factory()->create([
            'provider'       => 'car-and-driving',
            'make'           => 'Ford',
            'model'          => 'Ranger',
            'main_video_url' => 'https://cdn.caranddriving.com/ford-ranger.mp4',
        ]);

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json('data'));
    }

    public function testVideosEndpointExcludesNonStockCdVideoWhenStockExists(): void
    {
        // Create a C&D review with a linked active vehicle — should appear as stock, not non-stock
        $make    = VehicleMake::factory()->create(['name' => 'Ford', 'slug' => 'ford']);
        $model   = VehicleModel::factory()->create(['name' => 'Ranger', 'slug' => 'ranger', 'make_id' => $make->id]);
        $vehicle = Vehicle::factory()->create([
            'is_published'   => true,
            'main_video_url' => 'https://example.com/ranger-stock.mp4',
            'make_id'        => $make->id,
            'model_id'       => $model->id,
        ]);

        $review = VehicleReview::factory()->create([
            'provider'       => 'car-and-driving',
            'make'           => 'Ford',
            'model'          => 'Ranger',
            'main_video_url' => 'https://cdn.caranddriving.com/ranger.mp4',
        ]);
        $vehicle->reviews()->attach($review->id);

        $response = $this->getJson(route('vehicle-content.videos'));

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        // Should be the stock vehicle URL, not the review URL
        $this->assertEquals('https://example.com/ranger-stock.mp4', $response->json('data.0.video_url'));
    }

    // -------------------------------------------------------------------------
    // Filter options (makes / models)
    // -------------------------------------------------------------------------

    public function testMakesEndpointRequiresProvider(): void
    {
        $response = $this->getJson(route('vehicle-content.makes'));

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['provider']);
    }

    public function testMakesEndpointReturnsDistinctMakes(): void
    {
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'Ford',  'model' => 'Ranger']);
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'Ford',  'model' => 'Focus']);
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'BMW',   'model' => '3 Series']);
        VehicleReview::factory()->create(['provider' => 'other-provider',  'make' => 'Honda', 'model' => 'Civic']);

        $response = $this->getJson(route('vehicle-content.makes', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json());
        $this->assertContains('BMW', $response->json());
        $this->assertContains('Ford', $response->json());
        $this->assertNotContains('Honda', $response->json());
    }

    public function testModelsEndpointRequiresProvider(): void
    {
        $response = $this->getJson(route('vehicle-content.models'));

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['provider']);
    }

    public function testModelsEndpointReturnsDistinctModelsForProvider(): void
    {
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'Ford', 'model' => 'Ranger']);
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'Ford', 'model' => 'Focus']);
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'BMW',  'model' => '3 Series']);

        $response = $this->getJson(route('vehicle-content.models', ['provider' => 'car-and-driving']));

        $response->assertStatus(200);
        $this->assertCount(3, $response->json());
    }

    public function testModelsEndpointFiltersByMake(): void
    {
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'Ford', 'model' => 'Ranger']);
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'Ford', 'model' => 'Focus']);
        VehicleReview::factory()->create(['provider' => 'car-and-driving', 'make' => 'BMW',  'model' => '3 Series']);

        $response = $this->getJson(route('vehicle-content.models', [
            'provider' => 'car-and-driving',
            'make'     => 'Ford',
        ]));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json());
        $this->assertContains('Focus', $response->json());
        $this->assertContains('Ranger', $response->json());
        $this->assertNotContains('3 Series', $response->json());
    }
}
