<?php

namespace Tests\Tenant;

use Carbon\Carbon;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\VehicleReservations\Mail\FreeReservationCustomerMail;
use Mtc\VehicleReservations\Mail\FreeReservationExpiryReminderCustomerMail;
use Mtc\VehicleReservations\Mail\FreeReservationTeamMail;
use Mtc\VehicleReservations\Mail\VehicleMoveRequestedTeamMail;
use Mtc\VehicleReservations\Reservation;
use Mtc\VehicleReservations\VehicleMoveRequest;
use Tests\TenantTestCase;

/**
 * Guards against the mailables throwing while building the envelope or rendering the
 * view. The tenant here has no `mail-from-email` setting and no domain record, which is
 * what previously caused Address and Vehicle::getSiteUrlAttribute() type errors.
 */
class ReservationMailRenderTest extends TenantTestCase
{
    private function freeReservation(): Reservation
    {
        // A slug is required for Vehicle::getSiteUrlAttribute() to attempt URL resolution
        $vehicle = Vehicle::factory()->create(['slug' => 'sample-vehicle']);

        return Reservation::factory()->free()->create([
            'vehicle_id' => $vehicle->id,
            'status' => 'confirmed',
            'email' => 'customer@example.test',
            'expires_at' => Carbon::now()->addDays(3),
        ]);
    }

    public function testFreeReservationCustomerMailRenders(): void
    {
        $html = (new FreeReservationCustomerMail($this->freeReservation()))->render();

        $this->assertNotEmpty($html);
    }

    public function testFreeReservationTeamMailRenders(): void
    {
        $html = (new FreeReservationTeamMail($this->freeReservation()))->render();

        $this->assertNotEmpty($html);
    }

    public function testFreeReservationExpiryReminderMailRenders(): void
    {
        $html = (new FreeReservationExpiryReminderCustomerMail($this->freeReservation()))->render();

        $this->assertNotEmpty($html);
    }

    public function testMoveRequestTeamMailRenders(): void
    {
        $moveRequest = VehicleMoveRequest::factory()->paid()->create();

        $html = (new VehicleMoveRequestedTeamMail($moveRequest))->render();

        $this->assertNotEmpty($html);
    }

    public function testMailRendersWhenReservationHasNoVehicle(): void
    {
        $reservation = $this->freeReservation();
        $reservation->setRelation('vehicle', null);

        $html = (new FreeReservationCustomerMail($reservation))->render();

        $this->assertNotEmpty($html);
    }

    public function testMailRendersWhenReservationHasNoExpiry(): void
    {
        $reservation = $this->freeReservation();
        $reservation->expires_at = null;

        $html = (new FreeReservationCustomerMail($reservation))->render();

        $this->assertNotEmpty($html);
    }
}
