<?php

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

// Protect the auth endpoint with Sanctum so mobile clients can authenticate channels.
Broadcast::routes(['middleware' => ['auth:sanctum']]);

/*
 * Private channel for a specific chat group.
 * Only authenticated members of the group may subscribe.
 */
Broadcast::channel('chat.{groupId}', function ($user, int $groupId) {
    return ChatGroupMember::where('chat_group_id', $groupId)
        ->where('user_id', $user->id)
        ->exists();
});

/*
 * Private channel scoped to a single event.
 * Only users registered (non-cancelled) for the event may subscribe.
 */
Broadcast::channel('event.{eventId}', function ($user, int $eventId) {
    return $user->registrations()
        ->where('event_id', $eventId)
        ->where('status', '!=', 'cancelled')
        ->exists();
});
