<?php

namespace App\Mail;

use App\Models\Transaction;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Barryvdh\DomPDF\Facade\Pdf; // Import engine DomPDF

class SendCertificateMail extends Mailable
{
    use Queueable, SerializesModels;

    public $transaction;

    public function __construct(Transaction $transaction)
    {
        $this->transaction = $transaction;
    }

    public function build()
    {
        // 1. Render HTML sertifikat tadi dan ubah orientasinya jadi Landscape (Tidur) layaknya sertifikat asli
        $pdf = Pdf::loadView('emails.certificate', ['transaction' => $this->transaction])
                  ->setPaper('a4', 'landscape');

        // 2. Kirim email dengan melampirkan file PDF hasil render di atas
        return $this->subject('E-Certificate Resmi: ' . $this->transaction->event->title)
                    ->view('emails.certificate_notification') // Isi body email pemberitahuan
                    ->attachData($pdf->output(), 'Certificate-' . $this->transaction->order_id . '.pdf', [
                        'mime' => 'application/pdf',
                    ]);
    }
}