<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Headers;
use Illuminate\Queue\SerializesModels;

class DuePaymentsAlert extends Mailable
{
    use Queueable, SerializesModels;

    public string $userName;
    public array $overdueItems;
    public array $tomorrowItems;
    public array $accountBalances;
    public float $totalAvailable;
    public float $totalDue;
    public float $shortage;
    public array $payableItems;
    public array $unpayableItems;
    public array $transferSuggestions;
    public string $currency;

    /**
     * Create a new message instance.
     */
    public function __construct(
        string $userName,
        array $overdueItems,
        array $tomorrowItems,
        array $accountBalances,
        float $totalAvailable,
        float $totalDue,
        float $shortage,
        array $payableItems,
        array $unpayableItems,
        array $transferSuggestions,
        string $currency = 'EUR'
    ) {
        $this->userName = $userName;
        $this->overdueItems = $overdueItems;
        $this->tomorrowItems = $tomorrowItems;
        $this->accountBalances = $accountBalances;
        $this->totalAvailable = $totalAvailable;
        $this->totalDue = $totalDue;
        $this->shortage = $shortage;
        $this->payableItems = $payableItems;
        $this->unpayableItems = $unpayableItems;
        $this->transferSuggestions = $transferSuggestions;
        $this->currency = $currency;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        $subject = $this->shortage > 0
            ? '⚠️ Alerta: Pagamentos Vencidos - Saldo Insuficiente'
            : '📋 Lembrete: Pagamentos Pendentes';

        return new Envelope(
            from: new Address('no-reply@cnxifly.com', 'WEBMoney - ConneXiFly'),
            replyTo: [
                new Address('support@cnxifly.com', 'Soporte WEBMoney'),
            ],
            subject: $subject,
            tags: ['due-payments', 'alert'],
            metadata: [
                'total_due' => $this->totalDue,
                'shortage' => $this->shortage,
            ],
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.due-payments-alert',
            text: 'emails.due-payments-alert-text',
            with: [
                'userName' => $this->userName,
                'overdueItems' => $this->overdueItems,
                'tomorrowItems' => $this->tomorrowItems,
                'accountBalances' => $this->accountBalances,
                'totalAvailable' => $this->totalAvailable,
                'totalDue' => $this->totalDue,
                'shortage' => $this->shortage,
                'payableItems' => $this->payableItems,
                'unpayableItems' => $this->unpayableItems,
                'transferSuggestions' => $this->transferSuggestions,
                'currency' => $this->currency,
            ],
        );
    }

    /**
     * Extra headers to help deliverability.
     */
    public function headers(): Headers
    {
        return new Headers(
            text: [
                'X-Mailer' => 'WEBMoney/1.44.5',
                'X-Priority' => '3',
                'X-Auto-Response-Suppress' => 'OOF, AutoReply',
                'Precedence' => 'bulk',
                'List-Unsubscribe' => '<https://webmoney.cnxifly.com/preferences>, <mailto:no-reply@cnxifly.com?subject=unsubscribe>',
                'List-Unsubscribe-Post' => 'List-Unsubscribe=One-Click',
            ],
        );
    }

    /**
     * Get the attachments for the message.
     */
    public function attachments(): array
    {
        return [];
    }
}
