using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Meezi.Core.Constants;

namespace Meezi.API.Hubs;

[Authorize]
public class KdsHub : Hub
{
    public static string GroupName(string cafeId) => $"cafe:{cafeId}";

    public async Task JoinCafe(string cafeId)
    {
        var claimCafeId = Context.User?.FindFirst(MeeziClaimTypes.CafeId)?.Value;
        if (string.IsNullOrEmpty(claimCafeId) || claimCafeId != cafeId)
            throw new HubException("Forbidden");

        await Groups.AddToGroupAsync(Context.ConnectionId, GroupName(cafeId));
    }

    public Task LeaveCafe(string cafeId) =>
        Groups.RemoveFromGroupAsync(Context.ConnectionId, GroupName(cafeId));
}
