<?php

declare(strict_types=1);

namespace App\Notifications;

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

class TicketCustomerReplyNotification extends Notification implements ShouldQueue
{
    use Queueable;

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

    /** @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('Re: '.$this->ticket->subject.' '.$reference)
            ->greeting("Hello {$notifiable->name}!")
            ->line('A customer has replied to a support ticket.')
            ->line('**Ticket:** '.$this->ticket->subject)
            ->line('**Customer:** '.($this->reply->user?->name ?? $this->reply->from_email ?? 'Unknown'))
            ->line($this->reply->body)
            ->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_customer_reply',
            'ticket_id' => $this->ticket->id,
            'reply_id' => $this->reply->id,
            'subject' => $this->ticket->subject,
            'message' => 'Customer replied to ticket: '.$this->ticket->subject,
        ];
    }
}
