<?php

namespace App\Mail\Reservations;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

use Barryvdh\DomPDF\Facade\Pdf;

use App\Models\User;

use App\Models\Hotel\ExtraCharges;
use App\Models\Hotel\ConsumptionCharges;

use App\Helpers\Environment\SystemFeaturesHelper;
use App\Helpers\Support\PdfResourcesHelper;


class ReservationTicket extends Mailable
{
    use Queueable, SerializesModels;

    public $reservation;
    public $business;

    public $pdf;
    public $pdfName;

    public $logo_url;
    public $fallback_url;
    public $hotel_logo;

    public function __construct($reservation, $business)
    {
      // Parameters
        $this->reservation = $reservation;
        $this->business    = $business;

      // Local Variables
        $paper_size = [0, 0, 435, 650];

        $this->logo_url = optional(User::first())->profile_photo;
        $this->fallback_url = public_path('images/harsi_logo.png');

        // Global Variables
        $this->pdfName  = __('emails')['reservation_ticket']['title']." ".$this->reservation->id;
        $this->hotel_logo = PdfResourcesHelper::domPDFImageRender($this->logo_url, $this->fallback_url);

      // Control Variables
        $has_control_hours =  SystemFeaturesHelper::getFeatureStatus("control_hours");
        $has_express_reservations =  SystemFeaturesHelper::getFeatureStatus("express_reservations");
        $has_pax_pricing =  SystemFeaturesHelper::getFeatureStatus("pax_pricing");

        $this->pdf = Pdf::loadView('hotel.reservation-details.ticket-pdf', [
            'reservation' => $this->reservation,
            'business'    => $this->business,
            'display_signature' => false,
            'has_control_hours' => $has_control_hours,
            'has_express_reservations' => $has_express_reservations,
            'has_pax_pricing' => $has_pax_pricing,

            'title'       => $this->pdfName,
            'logo_src'    => $this->hotel_logo,
        ])->setPaper($paper_size)->setOptions([
            'defaultFont' => 'sans-serif',
            'isHtml5ParserEnabled' => true,
            'isRemoteEnabled'      => false
        ]);
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: config('app.name').' | '.__('entities')['hotel']['reservation']
                .' #'.$this->reservation->id.'(Ticket)',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.reservations.full-stay-ticket',
            with: [
                'reservation' => $this->reservation,
                'business'    => $this->business,
                'logo_src'    => $this->logo_url ?? $this->fallback_url,

                'has_control_hours' => SystemFeaturesHelper::getFeatureStatus("control_hours"),
            ],
        );
    }

    public function attachments(): array
    {
        return [
            Attachment::fromData(fn () => $this->pdf->output(), $this->pdfName.'.pdf')
                ->withMime('application/pdf'),
        ];
    }

}
