<?php

declare(strict_types=1);

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

/**
 * Critical alert email при отказе supplier-sync.
 *
 * Триггеры (Plan 3 Task 6 SyncSupplierProjectsJob):
 *   - sticky_auth: SupplierAuthException после retry через RefreshSupplierSessionJob
 *   - mass_transient: 50+ последовательных SupplierTransientException (подозрение на outage)
 *
 * Отправляется через config('services.supplier.alert_email') (Unisender Go SMTP relay).
 *
 * Spec: docs/superpowers/specs/2026-05-11-plan3-supplier-sync-design.md §4 Email alerts.
 */
final class SupplierCriticalAlertMail extends Mailable implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public readonly string $alertType,
        public readonly string $details,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "[Лидерра CRITICAL] Supplier sync: {$this->alertType}",
        );
    }

    public function content(): Content
    {
        return new Content(
            text: 'emails.supplier_alert_text',
            with: [
                'alertType' => $this->alertType,
                'details' => $this->details,
                'now' => now()->timezone('Europe/Moscow')->toIso8601String(),
            ],
        );
    }
}
