<?php

declare(strict_types=1);

namespace App\Notifications\Alerts;

use App\Models\AlertDelivery;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class EmailAlertNotification extends Notification
{
    use Queueable;

    /**
     * @param  array<string, mixed>  $payload
     */
    public function __construct(
        private readonly AlertDelivery $delivery,
        private readonly array $payload,
    ) {}

    /**
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $title = trim((string) ($this->payload['title'] ?? 'Alert'));
        $body = trim((string) ($this->payload['body'] ?? 'A matching alert event was detected.'));

        if ($title === '') {
            $title = 'Alert';
        }

        if ($body === '') {
            $body = 'A matching alert event was detected.';
        }

        return (new MailMessage)
            ->subject($title)
            ->line($body)
            ->line('Delivery ID: '.(int) $this->delivery->getKey())
            ->line('Event type: '.(string) $this->delivery->event_type)
            ->line('Status: '.(string) $this->delivery->status);
    }
}
