<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\VehicleReservations\Jobs\ProcessIncomingReservationJob;
use Mtc\VehicleReservations\Mail\FreeReservationCustomerMail;
use Mtc\VehicleReservations\Mail\FreeReservationTeamMail;
use Mtc\VehicleReservations\Reservation;
use Tests\TenantTestCase;

class FreeReservationConfirmationMailTest extends TenantTestCase
{
    private function configureTeamEmails(): void
    {
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Sales',
            'group' => 'General',
            'name' => 'Reservation Admin Team Emails',
            'config_key' => 'reservations-admin-team-emails',
            'type' => 'string',
            'value' => 'team@example.test',
        ]);
    }

    public function testFreeReservationSendsFreeSpecificConfirmationEmails()
    {
        Mail::fake();
        Http::fake();
        $this->configureTeamEmails();

        $vehicle = Vehicle::factory()->create();
        $reservation = Reservation::factory()->free()->create([
            'vehicle_id' => $vehicle->id,
            'status' => 'confirmed',
            'email' => 'customer@example.test',
            'confirmed_at' => Carbon::now()->subMinutes(5),
            'ingested_at' => null,
            'confirmation_sent_at' => null,
        ]);

        (new ProcessIncomingReservationJob($reservation))->handle();

        Mail::assertSent(FreeReservationCustomerMail::class);
        Mail::assertSent(FreeReservationTeamMail::class);
        Mail::assertNotSent(config('reservations.reservation_confirmation_mail'));
    }

    public function testStandardReservationStillUsesStandardConfirmationEmail()
    {
        Mail::fake();
        Http::fake();
        $this->configureTeamEmails();

        $vehicle = Vehicle::factory()->create();
        $reservation = Reservation::factory()->create([
            'vehicle_id' => $vehicle->id,
            'type' => Reservation::TYPE_STANDARD,
            'status' => 'confirmed',
            'email' => 'customer@example.test',
            'confirmed_at' => Carbon::now()->subMinutes(5),
            'ingested_at' => null,
            'confirmation_sent_at' => null,
        ]);

        (new ProcessIncomingReservationJob($reservation))->handle();

        Mail::assertSent(config('reservations.reservation_confirmation_mail'));
        Mail::assertNotSent(FreeReservationCustomerMail::class);
        Mail::assertNotSent(FreeReservationTeamMail::class);
    }
}
