<?php

declare(strict_types=1);

namespace App\Notifications\Messages;

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

final class NewMessageNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct(
        public Message $message
    ) {}

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['database', 'mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        $senderName = $this->message->sender->name;
        $propertyTitle = $this->message->conversation->property->title;

        return (new MailMessage)
            ->subject('New Message from '.$senderName)
            ->greeting('Hi '.$notifiable->name.'!')
            ->line($senderName.' sent you a message about '.$propertyTitle.':')
            ->line('"'.substr($this->message->message, 0, 100).'"')
            ->action('View Conversation', route('messages.show', $this->message->conversation_id))
            ->line('Reply directly from the link above!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'title' => 'New Message',
            'message' => $this->message->sender->name.' sent you a message about '.$this->message->conversation->property->title,
            'icon' => 'chat-bubble-left',
            'url' => route('messages.show', $this->message->conversation_id),
            'conversation_id' => $this->message->conversation_id,
            'sender_id' => $this->message->sender_id,
        ];
    }
}
