<?php

namespace App\Notifications;

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

class NewMessage extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct(
        private Conversation $conversation,
        private User $sender,
        private string $messageBody
    ) {
    }

    /**
     * Get the notification's delivery channels.
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        $url = route('messages.show', $this->conversation);

        // Preview of message (first 150 chars)
        $preview = strlen($this->messageBody) > 150
            ? substr($this->messageBody, 0, 150) . '...'
            : $this->messageBody;

        return (new MailMessage)
            ->subject("New Message from {$this->sender->name} - {$this->conversation->subject}")
            ->greeting("You have a new message!")
            ->line("From: {$this->sender->name}")
            ->line("Subject: {$this->conversation->subject}")
            ->line('')
            ->line("Message preview:")
            ->line("\"$preview\"")
            ->action('View Full Message', $url)
            ->line('Log in to Xeek to reply to this message.');
    }
}
