using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

namespace Shared.Excel;

[Authorize]
public sealed class ImportProgressHub : Hub
{
    public Task Subscribe(string sessionId) =>
        Groups.AddToGroupAsync(Context.ConnectionId, $"import:{sessionId}");

    public Task Unsubscribe(string sessionId) =>
        Groups.RemoveFromGroupAsync(Context.ConnectionId, $"import:{sessionId}");
}

public sealed class ImportProgressPublisher(IHubContext<ImportProgressHub> hub)
{
    public Task PublishAsync(ImportProgressEvent value, CancellationToken ct) =>
        hub.Clients.Group($"import:{value.SessionId}")
            .SendAsync("ImportProgress", value, ct);
}
