<?php

declare(strict_types=1);

namespace App\Mail;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

/**
 * Уведомление о пропавшем или постоянно падающем cron-задаче.
 *
 * Триггер: SchedulerCheckHeartbeats обнаружил:
 *   • отсутствие пульса > 2× ожидаемого интервала, ИЛИ
 *   • consecutive_failures >= 3.
 *
 * Отправляется на kdv1@bk.ru (monitoring email).
 */
final class SchedulerHeartbeatMissingMail extends Mailable
{
    public function __construct(
        public readonly string $commandName,
        public readonly string $reason,
        public readonly ?string $lastError,
        public readonly int $consecutiveFailures,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "[Лидерра HIGH] Scheduler heartbeat missing: {$this->commandName}",
        );
    }

    public function content(): Content
    {
        return new Content(
            text: 'emails.scheduler_heartbeat_missing_text',
            with: [
                'commandName' => $this->commandName,
                'reason' => $this->reason,
                'lastError' => $this->lastError,
                'consecutiveFailures' => $this->consecutiveFailures,
                'now' => now()->timezone('Europe/Moscow')->toIso8601String(),
            ],
        );
    }
}
