<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use App\Mail\VehicleSubscriptionNotificationMail;
use Illuminate\Database\Eloquent\Model;
use Mtc\ContentManager\Contracts\MediaUse;
use Mtc\ContentManager\Models\Media;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Enums\VehicleSubscriptionEventType;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleSubscription;
use Tests\TenantTestCase;

class VehicleSubscriptionNotificationMailTest extends TenantTestCase
{
    private function makeCustomer(): Model
    {
        $customerModel = config('crm.customer_model');

        return $customerModel::query()->create([
            'first_name' => 'Test',
            'last_name' => 'Customer',
            'email' => 'subscriber@example.com',
        ]);
    }

    private function makeSubscription(?Dealership $dealership = null): VehicleSubscription
    {
        $vehicle = Vehicle::factory()->create([
            'title' => 'Tested Hatchback',
            'dealership_id' => $dealership?->id,
        ]);

        $media = Media::factory()->create([
            'src' => 'avatar.jpg',
            'path' => 'path/to/directory/',
            'type' => 'image/jpeg',
        ]);

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

        return VehicleSubscription::query()->create([
            'customer_id' => $this->makeCustomer()->id,
            'vehicle_id' => $vehicle->id,
            'event_type' => VehicleSubscriptionEventType::PRICE_DROP->value,
        ]);
    }

    private function mailFor(VehicleSubscription $subscription): VehicleSubscriptionNotificationMail
    {
        return new VehicleSubscriptionNotificationMail(
            $subscription,
            VehicleSubscriptionEventType::PRICE_DROP
        );
    }

    public function testVehicleImageIsResolvedToAFullUrlNotTheRawFilename()
    {
        $subscription = $this->makeSubscription();

        $image = $this->mailFor($subscription)->content()->with['vehicle_image'];

        $this->assertNotEmpty($image);
        $this->assertStringContainsString('avatar.jpg', $image);
        // The old implementation passed the bare `src` ("avatar.jpg"); the new one resolves a URL.
        $this->assertNotEquals('avatar.jpg', $image);
    }

    public function testPhoneNumberComesFromTheVehiclesDealership()
    {
        Settings::update('app-phone-number-format', '5-6');

        $dealership = Dealership::factory()->create([
            'contact_no' => '01234567890',
        ]);

        $viewData = $this->mailFor($this->makeSubscription($dealership))->content()->with;

        $this->assertEquals('01234567890', $viewData['phone_number']);
        $this->assertEquals('01234 567890', $viewData['phone_number_formatted']);
    }

    public function testPhoneNumberIsNullWhenVehicleHasNoDealership()
    {
        $viewData = $this->mailFor($this->makeSubscription())->content()->with;

        $this->assertNull($viewData['phone_number']);
        $this->assertEquals('', $viewData['phone_number_formatted']);
    }

    public function testViewResolvesToTheCrmDefaultTemplateWhenNoThemeIsSet()
    {
        $view = $this->mailFor($this->makeSubscription())->content()->view;

        $this->assertEquals('crm::emails.vehicle-subscription-notification', $view);
    }

    public function testViewFallsBackToTheDefaultTemplateForAnUnknownTheme()
    {
        Settings::update('mail-customer-theme', 'a-theme-without-a-template');

        $view = $this->mailFor($this->makeSubscription())->content()->view;

        $this->assertEquals('crm::emails.vehicle-subscription-notification', $view);
    }

    public function testEmailRendersWithTheResolvedImageUrl()
    {
        $mail = $this->mailFor($this->makeSubscription());

        $mail->assertSeeInHtml('Tested Hatchback');
        $mail->assertSeeInHtml('avatar.jpg');
    }
}
