<?php
namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class EmployeeAlertEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $alertData;
    public $employeeId;

    /**
     * Create a new event instance.
     */
    public function __construct(array $alertData, string $employeeId)
    {
        $this->alertData = $alertData;
        $this->employeeId = $employeeId;
       
        // Debug logging
        \Log::info('EmployeeAlertEvent created', [
            'employee_id' => $employeeId,
            'channel' => 'employee.' . $employeeId,
            'event_name' => $this->broadcastAs()
        ]);
    }

    /**
     * Get the channels the event should broadcast on.
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('employee.' . $this->employeeId)
        ];
    }

    /**
     * The event's broadcast name.
     */
    public function broadcastAs(): string
    {
        return 'alert.received'; // Changed from 'test-event' to be more descriptive
    }

    /**
     * Get the data to broadcast.
     */
    public function broadcastWith(): array
    {
        return [
            'alert' => $this->alertData,
            'employee_id' => $this->employeeId,
            'action' => 'show_alert',
            'timestamp' => now()->toISOString()
        ];
    }

    /**
     * Determine if this event should broadcast.
     */
    public function shouldBroadcast(): bool
    {
        return true;
    }
}