<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;

class SystemNotification extends Notification
{
    public $title;
    public $message;
    public $actionUrl;
    public $iconType; // e.g., 'order', 'alert', 'success', 'chat'

    public function __construct($title, $message, $actionUrl = null, $iconType = 'alert')
    {
        $this->title = $title;
        $this->message = $message;
        $this->actionUrl = $actionUrl;
        $this->iconType = $iconType;
    }

    public function via(object $notifiable): array
    {
        // Send to Database and Reverb WebSocket INSTANTLY (Synchronously)
        return ['database', 'broadcast'];
    }

    public function toArray(object $notifiable): array
    {
        return [
            'title' => $this->title,
            'message' => $this->message,
            'action_url' => $this->actionUrl,
            'icon_type' => $this->iconType,
        ];
    }

    public function toBroadcast(object $notifiable): BroadcastMessage
    {
        return new BroadcastMessage([
            // Wrapping inside 'data' perfectly matches the API Database structure 
            // so the React Frontend doesn't break when reading instant WebSockets.
            'data' => [
                'title' => $this->title,
                'message' => $this->message,
                'action_url' => $this->actionUrl,
                'icon_type' => $this->iconType,
            ],
            'created_at' => now()->toIso8601String(),
        ]);
    }
}