<?php

namespace App\Mail;

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

class ProfessionalPayoutMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(public ProfessionalPayout $payout, public string $recipientName) {}

    public function envelope(): Envelope
    {
        return new Envelope(subject: "Payout Released — {$this->payout->reference} | DESXPERT");
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.professional-payout',
            with: [
                'name' => $this->recipientName,
                'payout' => $this->payout,
            ],
        );
    }

    public function attachments(): array
    {
        $attachments = [
            Attachment::fromData(
                fn () => view('emails.payout-invoice-attachment-text', ['payout' => $this->payout])->render(),
                "{$this->payout->reference}-invoice.txt",
            )->withMime('text/plain'),
        ];

        if ($this->payout->proof_path) {
            $attachments[] = Attachment::fromStorageDisk('payout_proofs', $this->payout->proof_path)
                ->as($this->payout->proof_original_name ?: 'proof-of-payment')
                ->withMime($this->payout->proof_mime_type ?: 'application/octet-stream');
        }

        return $attachments;
    }
}
