<?php

namespace App\Mail;

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

class LiteTemplateDeliveryMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public function __construct(public Payment $payment) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: '📦 NSMA Lite template — танай захиалга',
            replyTo: ['info@nsma.mn' => 'NSMA Зөвлөх'],
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.lite-delivery',
            with: ['payment' => $this->payment, 'lead' => $this->payment->lead],
        );
    }

    public function attachments(): array
    {
        $files = [];
        $pdfPath = public_path('downloads/templates/jdb-annual-marketing-plan-lite.pdf');
        $docxPath = public_path('downloads/templates/jdb-annual-marketing-plan-lite.docx');

        if (file_exists($pdfPath)) {
            $files[] = Attachment::fromPath($pdfPath)->as('NSMA-Lite-template.pdf')->withMime('application/pdf');
        }
        if (file_exists($docxPath)) {
            $files[] = Attachment::fromPath($docxPath)->as('NSMA-Lite-template.docx')->withMime('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        }
        return $files;
    }
}
