<?php

namespace App\Notifications;

use App\Models\Booking;
use Illuminate\Notifications\Notification;
use NotificationChannels\WebPush\WebPushChannel;
use NotificationChannels\WebPush\WebPushMessage;

class BookingCreatedPush extends Notification
{
    public function __construct(public Booking $booking) {}

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

    public function toWebPush(object $notifiable, Notification $notification): WebPushMessage
    {
        $booking = $this->booking->loadMissing(['customer', 'service', 'employee']);
        $customer = $booking->customer?->fullName() ?: 'Un cliente';
        $service = $booking->service?->name ?: 'un servicio';
        $time = $booking->scheduled_at?->format('d/m/Y H:i') ?: 'la fecha programada';

        return (new WebPushMessage)
            ->title('Nueva reserva')
            ->body("{$customer} reservó {$service} para {$time}.")
            ->icon('/icons/icon-192x192.png')
            ->badge('/icons/icon-192x192.png')
            ->tag('booking-created-'.$booking->id)
            ->data([
                'url' => route('tenant.bookings.show', $booking, false),
                'booking_id' => $booking->id,
                'code' => $booking->code,
            ])
            ->action('Ver reserva', 'open');
    }
}
