<?php

namespace App\Services;

use App\Mail\BookingReceiptMail;
use App\Models\Booking;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Mail;

class ReceiptService
{
    public function pdf(Booking $booking): \Barryvdh\DomPDF\PDF
    {
        $booking->loadMissing(['company', 'customer', 'driver', 'vehicleType', 'paymentMethod', 'currency']);

        return Pdf::loadView('admin.bookings.receipt', ['booking' => $booking]);
    }

    public function emailToCustomer(Booking $booking): void
    {
        if (! $booking->customer?->email) {
            return;
        }

        $pdfContent = $this->pdf($booking)->output();

        Mail::to($booking->customer->email)->queue(new BookingReceiptMail($booking, $pdfContent));
    }
}
