<?php

use App\Models\ChatParticipant;
use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('chat.{chatId}', function ($user, $chatId) {
    return ChatParticipant::where('chat_id', (int) $chatId)
        ->where('user_id', $user->id)
        ->where('is_blocked', false)
        ->exists();
});

/**
 * Private presence channel scoped to one user.
 * Used by the client to detect when a specific peer comes online.
 */
Broadcast::channel('presence-user.{userId}', function ($user, $userId) {
    if ($user->id !== (int) $userId) {
        return false;
    }

    // Return the data payload that Pusher/Soketi exposes to other subscribers.
    return [
        'id'     => $user->id,
        'name'   => $user->name,
        'status' => 'online',
    ];
});


Broadcast::channel('presence-online', function ($user) {
    return [
        'id'   => $user->id,
        'name' => $user->name,
    ];
});