<?php

namespace App\Mail;

use App\Models\MembershipCertificate;
use App\Models\Organization;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Queue\SerializesModels;

class MembershipCertificateMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public readonly MembershipCertificate $certificate
    ) {}

    public function envelope(): Envelope
    {
        $orgName = Organization::first()?->name ?? config('app.name');

        return new Envelope(
            subject: 'Your Membership Certificate — ' . $orgName,
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'membership.certificates.email',
            with: [
                'certificate' => $this->certificate,
                'member'      => $this->certificate->member,
            ],
        );
    }

    public function attachments(): array
    {
        if (!$this->certificate->pdf_path) {
            return [];
        }

        return [
            Attachment::fromStorageDisk('public', $this->certificate->pdf_path)
                ->as($this->certificate->certificate_number . '.pdf')
                ->withMime('application/pdf'),
        ];
    }
}
