<?php

declare(strict_types=1);

use App\Models\Conversation;
use App\Models\User;
use Illuminate\Support\Facades\Broadcast;

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

Broadcast::channel('conversation.{conversationId}', function (User $user, int $conversationId) {
    $conversation = Conversation::find($conversationId);

    if (! $conversation) {
        return false;
    }

    // User must be either the guest or the owner
    if ($user->id === $conversation->guest_id || $user->id === $conversation->owner_id) {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'avatar' => $user->profile_picture,
        ];
    }

    return false;
});
