<?php

namespace App\Mail\Audits;

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

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

use Barryvdh\DomPDF\Facade\Pdf;
use Dompdf\Dompdf;
use Dompdf\Options;

use App\Helpers\Validations\AuditHelper;

class AuditCompleted extends Mailable
{
    use Queueable, SerializesModels;

    //* Constants
    public $APP_URL;

    //* Dynamic Data
    public $business;
    public $audit;

    public function __construct($business, $audit)
    {
        $this->APP_URL = config('app.url');

        $this->business = $business;
        $this->audit = $audit;
    }

    public function build()
    {
        // Determinar estado de auditoría
        $is_concluded = $this->audit->is_concluded;
        $is_approved = $this->audit->is_approved;
        $audit_status = AuditHelper::getAuditStatus($is_concluded, $is_approved, $this->audit->rejection_reason, $this->audit->auditor);

        $include_restaurant = $this->audit->summary_restaurant_revenues !== null && isset($this->audit->income_confirmation_summary['restaurant']);
        $divide_restaurant_modules = $this->audit->summary_mini_shop_revenues !== null && isset($this->audit->income_confirmation_summary['mini_shop']);
        $include_petty_cash = $this->audit->petty_cash_summary !== null;

        $transaction_modules = ["hotel"];
            if($include_restaurant) array_push($transaction_modules, "restaurant");
            if($divide_restaurant_modules) array_push($transaction_modules, "mini_shop");

        // Imagen del logo
        if (str_starts_with(basename(base_path()), 'Harsi_Hoteleria')) {
            $img = 'public/images/harsi_logo.png'; // local
        } else {
            $img = 'server_images/harsi_logo.png'; // servidor
        }

        // Títulos del PDF
        $title = __('entities')['settings']['audit'];
        $PDFName = $title . " " . $this->audit->id;

        // Renderizar PDF desde vista `audits.report`
        $pdf = PDF::loadView('modules.audits.pdf.report', [
            'audit' => $this->audit,
            'audit_status' => $audit_status,
            'include_restaurant' => $include_restaurant,
            'divide_restaurant_modules' => $divide_restaurant_modules,
            'include_petty_cash' => $include_petty_cash,
            'transaction_modules' => $transaction_modules,
            'img' => $img,
            'title' => $title,
            'PDFName' => $PDFName,
        ])->setOptions(['defaultFont' => 'sans-serif']);

        return $this
            ->subject(config('app.name'). ' | ' .__('emails')['completed_audit']['cash_reconciliation'].' No.' . $this->audit->id . ' '. __('neutral_words')['completed'])
            ->view('emails.audit.completed') // Usa tu HTML personalizado
            ->with([
                'APP_URL' => $this->APP_URL,
                'business' => $this->business,
                'audit' => $this->audit,
            ])
            ->attachData($pdf->output(), $PDFName . '.pdf', [
                'mime' => 'application/pdf',
            ]);
    }
}
