<?php

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('chat.{receiverId}', function ($user, $receiverId) {
    return (int) $user->id === (int) $receiverId;
});

Broadcast::channel('notifications.{userId}', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
});

// WhatsApp chat — solo usuarios del mismo plantel que el prospecto pueden escuchar
Broadcast::channel('whatsapp-chat.{alumnoId}', function ($user, $alumnoId) {
    $alumno = \App\Models\Alumno::find($alumnoId);
    if (!$alumno) {
        return false;
    }
    $plantelesAlumno  = $alumno->alumnos()
        ->with('plantelUsuarios')
        ->get()
        ->flatMap(fn($u) => $u->plantelUsuarios->pluck('id'));
    $plantelesUsuario = $user->plantelUsuarios->pluck('id');
    return $plantelesAlumno->intersect($plantelesUsuario)->isNotEmpty();
});
