<?php

namespace App\Notifications;

use App\Models\Appointment;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\WebPush\WebPushChannel;
use NotificationChannels\WebPush\WebPushMessage;

class AppointmentCreatedNotification extends Notification
{
    use Queueable;

    public function __construct(
        public Appointment $appointment,
        public string $recipientRole
    ) {}

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

    public function toWebPush(object $notifiable, Notification $notification): WebPushMessage
    {
        $apt = $this->appointment;
        $clientName = $apt->client?->name ?? 'Cliente';
        $serviceName = $apt->service?->name ?? 'Servicio';
        $barberName = $apt->barber?->user?->name ?? 'Barbero';

        if ($this->recipientRole === 'barber') {
            return (new WebPushMessage)
                ->title('Nuevo turno asignado')
                ->body("{$clientName} - {$serviceName} el {$apt->appointment_date} a las {$apt->start_time}")
                ->icon('/logo.png')
                ->data(['appointment_id' => $apt->id, 'type' => 'appointment_created']);
        }

        return (new WebPushMessage)
            ->title('Reserva confirmada')
            ->body("{$serviceName} con {$barberName} el {$apt->appointment_date} a las {$apt->start_time}")
            ->icon('/logo.png')
            ->data(['appointment_id' => $apt->id, 'type' => 'appointment_confirmed']);
    }
}
