<?php

namespace App\Mail;

use App\Models\EmailTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class TemplatedMail extends Mailable
{
    use SerializesModels;

    public string $renderedSubject;
    public string $renderedBody;

    public function __construct(
        public EmailTemplate $template,
        public array $data = []
    ) {
        $this->renderedSubject = $template->renderSubject($data);
        $this->renderedBody = $template->renderBody($data);
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: $this->renderedSubject,
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.templated',
            with: [
                'body' => $this->renderedBody,
                'data' => $this->data,
            ],
        );
    }

    public function attachments(): array
    {
        return [];
    }
}
