<?php

namespace Tests\Tenant;

use App\Modules\ImageSync\AutoImaging;
use Mtc\ContentManager\Contracts\MediaUse;
use Mtc\ContentManager\Models\Media;
use Mtc\MercuryDataModels\ApiNotification;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class AutoImagingRetryTest extends TenantTestCase
{
    public function testSyncAppliesVimeoUrlFromUnprocessedNotification(): void
    {
        $vehicle = Vehicle::factory()->create(['vrm_condensed' => 'AB12CDE']);
        $this->makeNotification($vehicle, ['vimeoUrl' => 'https://vimeo.com/main']);

        new AutoImaging()->sync();

        $this->assertSame('https://vimeo.com/main', $vehicle->fresh()->main_video_url);
    }

    public function testSyncAppliesVideoUrlFromUnprocessedNotification(): void
    {
        $vehicle = Vehicle::factory()->create(['vrm_condensed' => 'AB12CDE']);
        $this->makeNotification($vehicle, ['videoUrl' => 'https://example.com/main.mp4']);

        new AutoImaging()->sync();

        $this->assertSame('https://example.com/main.mp4', $vehicle->fresh()->main_video_url);
    }

    public function testSyncAppliesInteriorVideoUrlFromUnprocessedNotification(): void
    {
        $vehicle = Vehicle::factory()->create(['vrm_condensed' => 'AB12CDE']);
        $this->makeNotification($vehicle, ['Urls' => ['https://example.com/interior.mp4']]);

        new AutoImaging()->sync();

        $this->assertSame('https://example.com/interior.mp4', $vehicle->fresh()->interior_video_url);
    }

    public function testSyncMarksVideoOnlyNotificationAsProcessed(): void
    {
        $vehicle = Vehicle::factory()->create(['vrm_condensed' => 'AB12CDE']);
        $notification = $this->makeNotification($vehicle, ['vimeoUrl' => 'https://vimeo.com/main']);

        new AutoImaging()->sync();

        $this->assertTrue($notification->fresh()->processed);
    }

    public function testRetryCommandAppliesAllVideoFields(): void
    {
        $vehicle = Vehicle::factory()->create(['registration_number' => 'AB12CDE']);
        $notification = ApiNotification::query()->create([
            'provider' => 'auto-imaging',
            'reference' => 'AB12CDE',
            'data' => [
                'vimeoUrl' => 'https://vimeo.com/main',
                'videoUrl' => 'https://example.com/main.mp4',
                'Urls' => ['https://example.com/interior.mp4'],
            ],
            'headers' => [],
            'data_model' => 'vehicle',
        ]);

        $this->artisan('retry:auto-imaging')->assertSuccessful();

        $fresh = $vehicle->fresh();
        // videoUrl applied last, so it wins over vimeoUrl for main_video_url
        $this->assertSame('https://example.com/main.mp4', $fresh->main_video_url);
        $this->assertSame('https://example.com/interior.mp4', $fresh->interior_video_url);
        // Command does not mark notifications processed; that's by design (forces reprocessing).
        $this->assertNull($notification->fresh()->processed);
    }

    public function testRetryCommandAppliesVideoEvenWhenVehicleHasExistingMedia(): void
    {
        $vehicle = Vehicle::factory()->create(['registration_number' => 'AB12CDE']);
        $media = Media::factory()->create();
        MediaUse::factory()->create([
            'media_id' => $media->id,
            'owner_type' => $vehicle->getMorphClass(),
            'owner_id' => $vehicle->id,
        ]);
        $this->assertSame(1, $vehicle->mediaUses()->count());

        ApiNotification::query()->create([
            'provider' => 'auto-imaging',
            'reference' => 'AB12CDE',
            'data' => ['vimeoUrl' => 'https://vimeo.com/main'],
            'headers' => [],
            'data_model' => 'vehicle',
        ]);

        $this->artisan('retry:auto-imaging')->assertSuccessful();

        $this->assertSame('https://vimeo.com/main', $vehicle->fresh()->main_video_url);
    }

    private function makeNotification(Vehicle $vehicle, array $data): ApiNotification
    {
        return ApiNotification::query()->create([
            'provider' => 'auto-imaging',
            'reference' => $vehicle->vrm_condensed,
            'data' => $data,
            'headers' => [],
            'data_model' => 'vehicle',
        ]);
    }
}
