<?php

declare(strict_types=1);

namespace App\Mail;

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

/**
 * Уведомление о разрыве hash-chain в audit-таблице.
 *
 * Триггер: команда audit:verify-chains обнаружила несовпадение
 * stored vs recomputed SHA-256 hash — признак tampering.
 *
 * Отправляется на kdv1@bk.ru (monitoring email).
 */
final class AuditChainBreachMail extends Mailable
{
    public function __construct(
        public readonly string $tableName,
        public readonly int $firstBrokenId,
        public readonly int $mismatchCount,
        public readonly ?string $partitionName = null,  // v8.31: partition where breach was detected
    ) {}

    public function envelope(): Envelope
    {
        $subject = $this->partitionName !== null && $this->partitionName !== $this->tableName
            ? "[Лидерра CRITICAL] Разрыв hash-chain в {$this->partitionName}"
            : "[Лидерра CRITICAL] Разрыв hash-chain в {$this->tableName}";

        return new Envelope(subject: $subject);
    }

    public function content(): Content
    {
        return new Content(
            text: 'emails.audit_chain_breach_text',
            with: [
                'tableName' => $this->tableName,
                'partitionName' => $this->partitionName ?? $this->tableName,
                'firstBrokenId' => $this->firstBrokenId,
                'mismatchCount' => $this->mismatchCount,
                'now' => now()->timezone('Europe/Moscow')->toIso8601String(),
            ],
        );
    }
}
