<?php

// 5. Event Broadcasting for Real-time Updates
namespace App\Events;

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

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

    public $user;
    public $guard;
    public $status; // 'online' or 'offline'

    public function __construct($user, $guard, $status = 'online')
    {
        $this->user = $user;
        $this->guard = $guard;
        $this->status = $status;
    }

    public function broadcastOn()
    {
        return new PresenceChannel("presence-{$this->guard}-users");
    }

    public function broadcastWith()
    {
        return [
            'user' => [
                'id' => $this->user->id,
                'name' => $this->user->name,
                'avatar' => $this->user->avatar ?? null,
            ],
            'guard' => $this->guard,
            'status' => $this->status,
            'timestamp' => now(),
        ];
    }
}
