<?php

namespace Tests\Tenant;

use App\Mail\NewEnquiryCustomerCopyNotification;
use Mtc\ContentManager\Models\Media;
use Mtc\ContentManager\Models\MediaUse;
use Mtc\Crm\Models\Enquiry;
use Mtc\Crm\Models\Form;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class NewEnquiryCustomerCopyNotificationTest extends TenantTestCase
{
    public function testVehicleImageIsExposedWhenEnquiryRelatesToVehicle(): void
    {
        $vehicle = Vehicle::factory()->create();
        $media = Media::factory()->create(['src' => 'foo.png', 'type' => 'image']);
        MediaUse::factory()->create([
            'media_id' => $media->id,
            'owner_type' => 'vehicle',
            'owner_id' => $vehicle->id,
            'primary' => 1,
        ]);

        $form = Form::factory()->create();
        $enquiry = Enquiry::factory()->create(['form_id' => $form->id]);
        $enquiry->reason()->associate($vehicle);
        $enquiry->save();

        $viewData = new NewEnquiryCustomerCopyNotification($enquiry->fresh())->content()->with;

        $expected = $vehicle->primaryMediaUse?->getUrl('large')
            ?? $vehicle->primaryMediaUse?->media?->getOriginalUrlAttribute();

        $this->assertArrayHasKey('vehicle_image', $viewData);
        $this->assertNotEmpty($expected);
        $this->assertSame($expected, $viewData['vehicle_image']);
    }

    public function testVehicleImageIsNullWhenEnquiryHasNoVehicle(): void
    {
        $form = Form::factory()->create();
        $enquiry = Enquiry::factory()->create(['form_id' => $form->id]);

        $viewData = new NewEnquiryCustomerCopyNotification($enquiry->fresh())->content()->with;

        $this->assertArrayHasKey('vehicle_image', $viewData);
        $this->assertNull($viewData['vehicle_image']);
    }
}
