<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use App\Mail\NewBookingConfirmationMail;
use Illuminate\Support\Str;
use Mtc\MercuryDataModels\Booking;
use Mtc\MercuryDataModels\Dealership;
use Tests\TenantTestCase;

class NewBookingConfirmationMailTest extends TenantTestCase
{
    private function makeDealership(array $attributes = []): Dealership
    {
        return Dealership::factory()->create(array_merge([
            'name' => 'Batchelors Skipton',
            'contact_no' => '01756123456',
            'address1' => 'Gargrave Road',
            'address2' => '',
            'city' => 'Skipton',
            'county' => 'North Yorkshire',
            'postcode' => 'BD23 1UD',
        ], $attributes));
    }

    private function makeBooking(?Dealership $dealership = null): Booking
    {
        return Booking::factory()->create([
            'uuid' => Str::random(16),
            'location_id' => $dealership?->id,
            'email' => 'customer@example.com',
        ]);
    }

    private function useBatchelorsTheme(): void
    {
        Settings::update('mail-customer-theme', 'batchelors');
        Settings::update('app-contact-phone_number', '01943111222');
        Settings::update('app-phone-number-format', '5-6');
    }

    public function testTemplateResolvesToTheBatchelorsBookingTemplate()
    {
        $this->useBatchelorsTheme();

        $view = (new NewBookingConfirmationMail($this->makeBooking()))->content()->view;

        $this->assertEquals('crm::emails.batchelors.new-booking-customer', $view);
    }

    public function testHeaderAndFooterUseTheBookingLocationPhoneNumber()
    {
        $this->useBatchelorsTheme();

        $mail = new NewBookingConfirmationMail($this->makeBooking($this->makeDealership()));

        $mail->assertSeeInHtml('01756 123456', false);
        $mail->assertSeeInHtml('tel:01756123456', false);
        // The site-wide fallback number must not leak in once the booking has a location.
        $mail->assertDontSeeInHtml('01943 111222', false);
    }

    public function testFooterUsesTheBookingLocationAddress()
    {
        $this->useBatchelorsTheme();

        $mail = new NewBookingConfirmationMail($this->makeBooking($this->makeDealership()));

        $mail->assertSeeInHtml('Batchelors Skipton', false);
        $mail->assertSeeInHtml('Gargrave Road', false);
        $mail->assertSeeInHtml('Skipton', false);
        $mail->assertSeeInHtml('North Yorkshire', false);
        $mail->assertSeeInHtml('BD23 1UD', false);
        // The previously hardcoded head-office address must no longer be rendered.
        $mail->assertDontSeeInHtml('Kineholme Garage', false);
    }

    public function testEmptyAddressPartsAreSkippedRatherThanLeavingBlankLines()
    {
        $this->useBatchelorsTheme();

        $dealership = $this->makeDealership(['address2' => '', 'county' => '']);

        $mail = new NewBookingConfirmationMail($this->makeBooking($dealership));

        $mail->assertSeeInHtml('Gargrave Road,', false);
        $mail->assertDontSeeInHtml(',<br/>,', false);
    }

    public function testFallsBackToSiteDetailsWhenTheBookingHasNoLocation()
    {
        $this->useBatchelorsTheme();

        $mail = new NewBookingConfirmationMail($this->makeBooking());

        $mail->assertSeeInHtml('01943 111222', false);
        $mail->assertSeeInHtml('Kineholme Garage', false);
    }

    public function testFallsBackToTheSitePhoneNumberWhenTheLocationHasNone()
    {
        $this->useBatchelorsTheme();

        $dealership = $this->makeDealership(['contact_no' => '']);

        $mail = new NewBookingConfirmationMail($this->makeBooking($dealership));

        // Address still comes from the location, but the number falls back rather than rendering blank.
        $mail->assertSeeInHtml('Gargrave Road', false);
        $mail->assertSeeInHtml('01943 111222', false);
    }
}
