<?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 TicketStatusChangedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public SupportTicket $ticket,
        public string $oldStatus,
        public string $newStatus,
    ) {}

    /** @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.']';
        $accountUrl = 'https://'.config('app.domains.account').'/tickets/'.$this->ticket->id;
        $formattedStatus = ucwords(str_replace('_', ' ', $this->newStatus));

        return (new MailMessage)
            ->subject($this->ticket->subject.' '.$reference.' - Status: '.$formattedStatus)
            ->greeting('Hello '.$notifiable->name.'!')
            ->line('The status of your support ticket has been updated.')
            ->line('**Ticket:** '.$this->ticket->subject)
            ->line('**New Status:** '.$formattedStatus)
            ->action('View Ticket', $accountUrl)
            ->line('Thank you for using EZSCALE support!');
    }

    /** @return array<string, mixed> */
    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'ticket_status_changed',
            'ticket_id' => $this->ticket->id,
            'subject' => $this->ticket->subject,
            'old_status' => $this->oldStatus,
            'new_status' => $this->newStatus,
            'message' => 'Ticket "'.$this->ticket->subject.'" status changed to '.ucwords(str_replace('_', ' ', $this->newStatus)),
        ];
    }
}
