<?php

declare(strict_types=1);

namespace App\Mail;

use Carbon\CarbonInterface;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

/**
 * Email алерт админу Лидерры о расхождении CSV-сверки > 5%.
 *
 * Spec: docs/superpowers/specs/2026-05-11-plan4-billing-csv-admin-design.md §5.6
 */
final class CsvDriftAlertMail extends Mailable
{
    use Queueable;
    use SerializesModels;

    public function __construct(
        public readonly int $reconcileLogId,
        public readonly int $totalCsvRows,
        public readonly int $missingCount,
        public readonly int $recoveredCount,
        public readonly float $driftRatio,
        public readonly CarbonInterface $windowStart,
        public readonly CarbonInterface $windowEnd,
    ) {}

    public function envelope(): Envelope
    {
        $pct = number_format($this->driftRatio * 100, 2, ',', ' ');
        $window = $this->windowStart->format('Y-m-d H:i').' — '.$this->windowEnd->format('Y-m-d H:i');

        return new Envelope(
            subject: "Лидерра ↔ Поставщик: расхождение CSV > 5% за {$window} ({$pct}%)",
        );
    }

    public function content(): Content
    {
        return new Content(view: 'emails.csv_drift_alert');
    }
}
