<?php

namespace App\Notifications;

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

class BuyerInterest extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct(
        private Item $item,
        private User $buyer,
        private int $position
    ) {
    }

    /**
     * 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
    {
        $isPrimary = $this->position === 1;

        if ($isPrimary) {
            $subject = "New Buyer for Your Item: {$this->item->title}";
            $greeting = "You have a new buyer!";
            $message = "{$this->buyer->name} is interested in buying your item and is now the primary buyer.";
            $action = 'View Item & Buyer';
        } else {
            $subject = "Backup Buyer for Your Item: {$this->item->title}";
            $greeting = "You have a backup buyer!";
            $message = "{$this->buyer->name} is interested in your item and is backup buyer #" . ($this->position - 1) . ".";
            $action = 'View Item & Buyers';
        }

        $url = route('items.manage-queue', $this->item);

        return (new MailMessage)
            ->subject($subject)
            ->greeting($greeting)
            ->line($message)
            ->line("Item: {$this->item->title}")
            ->line("Buyer: {$this->buyer->name}")
            ->action($action, $url)
            ->line('Log in to your Xeek account to manage interested buyers.');
    }
}
