<?php

namespace Daniv04\HaciendaPackage\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Daniv04\HaciendaPackage\Models\Receipt;
use Daniv04\HaciendaPackage\Traits\XmlBase64DecoderTrait;

class InvoiceAcceptedMail extends Mailable
{
    use Queueable, SerializesModels;
    use XmlBase64DecoderTrait;

    public function __construct(
        public Receipt $receipt,
        public string  $pdfBytes
    ) {}

    public function build(): static
    {
        $consecutive = $this->receipt->ui_key;
        $xml= $this->decodeBase64Xml($this->receipt->signed_xml);

        $mail = $this
            ->from(config('invoicing.email_from_address'), config('invoicing.email_from_name'))
            ->subject('Factura Electrónica - ' . $consecutive)
            ->view('hacienda-package::emails.invoice_accepted')
            ->attachData($this->pdfBytes, "{$consecutive}.pdf", [
                'mime' => 'application/pdf',
            ])
            ->attachData($xml, "{$consecutive}.xml", [
                'mime' => 'application/xml',
            ]);

        return $mail;
    }
}
