<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NewNotification implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public int    $userId,
        public string $title,
        public string $body,
        public string $icon  = 'fa-bell',
        public string $color = 'primary',
        public ?string $url  = null,
    ) {}

    public function broadcastOn(): array
    {
        return [new Channel('notifications.' . $this->userId)];
    }

    public function broadcastWith(): array
    {
        return [
            'title' => $this->title,
            'body'  => $this->body,
            'icon'  => $this->icon,
            'color' => $this->color,
            'url'   => $this->url,
            'time'  => now()->format('H:i'),
        ];
    }
}
