<?php

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

use App\Models\Livestream;

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

Broadcast::channel('role.{name}', function ($user, $name) {
    return $user->hasRole($name);
});

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

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

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

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

Broadcast::channel('ce.{uuid}', function ($user, $uuid) {
    if ($user) {
        return [
            'id' => $user->id,
            'name' => $user->name
        ];
    }
});


Broadcast::channel('livestream.{livestream}', function ($user, Livestream $livestream) {
    if ($user->can('chat', $livestream)) {
        return [
            'id' => $user->id,
            'name' => $user->name,
        ];
    }
});

Broadcast::channel('livestream.{livestream}.moderator', function ($user, Livestream $livestream) {
    return $user->can('moderate', $livestream);
});
