<?php

namespace App\Mail;

use App\Models\AlertIncident;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

/** Queued alert e-mail — one per fire/resolve transition. Plain text (no external assets). */
class AlertNotification extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public function __construct(
        public AlertIncident $incident,
        public bool $resolved,
    ) {}

    public function envelope(): Envelope
    {
        $server = $this->incident->server?->name ?? '—';
        $rule = $this->incident->rule?->name ?? '—';
        $prefix = $this->resolved ? __('alerts.mail_subject_resolved') : __('alerts.mail_subject_firing');

        return new Envelope(subject: "[{$prefix}] {$rule} — {$server}");
    }

    public function content(): Content
    {
        return new Content(
            text: 'mail.alert',
            with: [
                'incident' => $this->incident,
                'resolved' => $this->resolved,
                'server' => $this->incident->server?->name ?? '—',
                'rule' => $this->incident->rule?->name ?? '—',
                'metric' => $this->incident->rule?->metric ?? '—',
                'value' => $this->incident->value,
                'threshold' => $this->incident->rule?->threshold ?? 0,
            ],
        );
    }
}
