<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\Events\JobFailed;

class QueueFailureNotification extends Notification
{
    public function __construct(
        public JobFailed $event
    ) {}

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $jobName = $this->event->job->resolveName();
        $environment = app()->environment();

        return (new MailMessage)
            ->subject("[URGENT] [{$environment}] Queue Job Failed: {$jobName} - Libra Law Firm")
            ->greeting('Queue Failure Alert')
            ->line('A queued job has failed.')
            ->line("**Job:** {$jobName}")
            ->line('**Queue:** '.$this->event->job->getQueue())
            ->line('**Error:** '.$this->event->exception->getMessage())
            ->line('**Time:** '.now()->format('Y-m-d H:i:s'))
            ->line('**Attempts:** '.$this->event->job->attempts())
            ->line('Please check the failed_jobs table and application logs for details.')
            ->salutation('— Libra System Monitor');
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'queue_failure',
            'job' => $this->event->job->resolveName(),
            'queue' => $this->event->job->getQueue(),
        ];
    }
}
