using Microsoft.AspNetCore.SignalR;

namespace ZB.MOM.WW.OtOpcUa.Admin.Hubs;

/// <summary>
///     Pushes per-node generation-apply state changes (<c>ClusterNodeGenerationState</c>) to
///     subscribed browser clients. Clients call <c>SubscribeCluster(clusterId)</c> on connect to
///     scope notifications; the server sends <c>NodeStateChanged</c> messages whenever the poller
///     observes a delta.
/// </summary>
public sealed class FleetStatusHub : Hub
{
    public Task SubscribeCluster(string clusterId)
    {
        if (string.IsNullOrWhiteSpace(clusterId)) return Task.CompletedTask;
        return Groups.AddToGroupAsync(Context.ConnectionId, GroupName(clusterId));
    }

    public Task UnsubscribeCluster(string clusterId)
    {
        if (string.IsNullOrWhiteSpace(clusterId)) return Task.CompletedTask;
        return Groups.RemoveFromGroupAsync(Context.ConnectionId, GroupName(clusterId));
    }

    /// <summary>Clients call this once to also receive fleet-wide status — used by the dashboard.</summary>
    public Task SubscribeFleet() => Groups.AddToGroupAsync(Context.ConnectionId, FleetGroup);

    public const string FleetGroup = "__fleet__";
    public static string GroupName(string clusterId) => $"cluster:{clusterId}";
}

public sealed record NodeStateChangedMessage(
    string NodeId,
    string ClusterId,
    long? CurrentGenerationId,
    string? LastAppliedStatus,
    string? LastAppliedError,
    DateTime? LastAppliedAt,
    DateTime? LastSeenAt);
