<?php

use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Broadcast;
use Namu\WireChat\Helpers\MorphClassResolver;
use Namu\WireChat\Models\Conversation;

/*
|--------------------------------------------------------------------------
| 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.
|
*/

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


// Test private broadcast: $toUserId is provided in web.php route
Broadcast::channel('change-lang.{toUserId}', function ($user, $toUserId) {
    return (int) $user->id == (int) $toUserId;
});


Broadcast::channel('switch-profile.{userId}', function ($user, $userId) {
    // The switch-profile channel is always subscribed using the web user's ID
    // (from SwitchProfile component's getListeners method using Auth::guard('web')->id())
    // We need to authorize against the web user, not the active profile
    $webUser = Auth::guard('web')->user();

    if (!$webUser) {
        return false;
    }

    return (int) $webUser->id === (int) $userId;
}, [
    'guards' => ['admin', 'bank', 'organization', 'web'],
]);



// Override WireChat conversation channel
Broadcast::channel('conversation.{conversationId}', function ($user, $conversationId) {

    $activeGuard = session('active_guard');
    $actor = null;

    // If no active_guard is set, default to 'web' guard
    if (!$activeGuard) {
        $activeGuard = 'web';
    }

    if ($activeGuard && in_array($activeGuard, ['admin', 'bank', 'organization', 'web'])) {
        $actor = Auth::guard($activeGuard)->user();
    }

    $conversation = Conversation::find($conversationId);

    if ($actor && $conversation && $actor->belongsToConversation($conversation)) {
        return true;
    }
    return false;
}, [
    'guards' => ['admin', 'bank', 'organization', 'web'],
    'middleware' => ['auth.any:admin,bank,organization,web'],
]);


// Override WireChat participant channel
Broadcast::channel('participant.{encodedType}.{id}', function ($user, $encodedType, $id) {
    $activeGuard = session('active_guard');
    $actor = null;

    // If no active_guard is set, default to 'web' guard
    if (!$activeGuard) {
        $activeGuard = 'web';
    }

    if ($activeGuard && in_array($activeGuard, ['admin', 'bank', 'organization', 'web'])) {
        $actor = Auth::guard($activeGuard)->user();
    }

    $morphType = MorphClassResolver::decode($encodedType);

    return $actor && $actor->id == $id && $actor->getMorphClass() == $morphType;
}, [
    'guards' => ['admin', 'bank', 'organization', 'web'],
    'middleware' => ['auth.any:admin,bank,organization,web'],
]);


// Presence Channel Authorization
Broadcast::channel('presence-{guard}-users', function ($user, $guard) {
    if (auth($guard)->check()) {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'avatar' => $user->avatar ?? null,
            'guard' => $guard,
            'joined_at' => now(),
        ];
    }
    return false;
});


//Private channel for presence updates
Broadcast::channel('presence-updates-{guard}', function ($user, $guard) {
    return auth($guard)->check();
});

// Private channel for forced logout
Broadcast::channel('user.logout.{userId}', function ($user, $userId) {
    return (int) $user->id === (int) $userId;
});

// Broadcast::channel('redirect-channel', function ($user) {
//     return !is_null($user);
// });

