<?php

namespace App\Notifications;

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

class OfferReceived extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(private Offer $offer)
    {
    }

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        $offerType = $this->offer->is_committed ? 'Committed Offer' : 'No-Commit Offer';
        $url = route('items.show', $this->offer->item);

        return (new MailMessage)
            ->subject("New $offerType on Your Item: {$this->offer->item->title}")
            ->greeting("You received a new offer!")
            ->line("$offerType from {$this->offer->buyer->name}")
            ->line("Item: {$this->offer->item->title}")
            ->line("Offered Price: \$" . number_format($this->offer->offered_price, 2))
            ->when($this->offer->message, function ($mail) {
                return $mail->line("Buyer's Message: \"{$this->offer->message}\"");
            })
            ->line('')
            ->line("Original Price: \$" . number_format($this->offer->item->price, 2))
            ->action('View Offer & Item', $url)
            ->line('Log in to accept, counter, or reject this offer.');
    }
}
