<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\SupportTicket;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class TicketCreatedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public SupportTicket $ticket,
    ) {}

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

    public function toMail(object $notifiable): MailMessage
    {
        $reference = $this->ticket->ticket_reference ?? '[EZSCALE-'.$this->ticket->id.']';
        $adminUrl = 'https://'.config('app.domains.admin').'/tickets/'.$this->ticket->id;

        return (new MailMessage)
            ->subject('New Ticket: '.$this->ticket->subject.' '.$reference)
            ->greeting("Hello {$notifiable->name}!")
            ->line('A new support ticket has been created.')
            ->line('**Subject:** '.$this->ticket->subject)
            ->line('**Priority:** '.ucfirst($this->ticket->priority))
            ->line('**Department:** '.ucfirst($this->ticket->department))
            ->line('**Customer:** '.($this->ticket->user?->name ?? $this->ticket->customer_email ?? 'Unknown'))
            ->action('View Ticket', $adminUrl)
            ->line('Please review and respond at your earliest convenience.');
    }

    /** @return array<string, mixed> */
    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'ticket_created',
            'ticket_id' => $this->ticket->id,
            'subject' => $this->ticket->subject,
            'priority' => $this->ticket->priority,
            'department' => $this->ticket->department,
            'message' => 'New support ticket: '.$this->ticket->subject,
        ];
    }
}
