namespace ZB.MOM.WW.MxGateway.Server.Dashboard;

/// <summary>
///     Snapshot of the Galaxy Repository (ZB) browse state surfaced on the dashboard.
///     Built by <see cref="DashboardGalaxySummaryProjector"/> from the shared-library Galaxy
///     hierarchy cache on each dashboard snapshot; the projector memoizes only the O(N)
///     <see cref="GalaxyObjectBreakdown"/> on the cache sequence while copying the volatile
///     status/timestamp fields fresh, so the dashboard never blocks on SQL.
/// </summary>
public sealed record DashboardGalaxySummary(
    DashboardGalaxyStatus Status,
    DateTimeOffset? LastQueriedAt,
    DateTimeOffset? LastSuccessAt,
    DateTimeOffset? LastDeployTime,
    string? LastError,
    int ObjectCount,
    int AreaCount,
    int AttributeCount,
    int HistorizedAttributeCount,
    int AlarmAttributeCount,
    IReadOnlyList<DashboardGalaxyTemplateUsage> TopTemplates,
    IReadOnlyList<DashboardGalaxyCategoryCount> ObjectCategories)
{
    /// <summary>Gets the unknown Galaxy status placeholder.</summary>
    public static DashboardGalaxySummary Unknown { get; } = new(
        DashboardGalaxyStatus.Unknown,
        LastQueriedAt: null,
        LastSuccessAt: null,
        LastDeployTime: null,
        LastError: null,
        ObjectCount: 0,
        AreaCount: 0,
        AttributeCount: 0,
        HistorizedAttributeCount: 0,
        AlarmAttributeCount: 0,
        TopTemplates: Array.Empty<DashboardGalaxyTemplateUsage>(),
        ObjectCategories: Array.Empty<DashboardGalaxyCategoryCount>());
}

public enum DashboardGalaxyStatus
{
    Unknown = 0,
    Healthy = 1,
    Stale = 2,
    Unavailable = 3,
}

public sealed record DashboardGalaxyTemplateUsage(string TemplateName, int InstanceCount);

public sealed record DashboardGalaxyCategoryCount(int CategoryId, string CategoryName, int ObjectCount);

/// <summary>
///     The O(N)-computed template-usage and category breakdown of a Galaxy hierarchy cache entry —
///     the only part of <see cref="DashboardGalaxySummary"/> that changes with the cache sequence
///     and is therefore safe to memoize on it.
/// </summary>
public sealed record GalaxyObjectBreakdown(
    IReadOnlyList<DashboardGalaxyTemplateUsage> TopTemplates,
    IReadOnlyList<DashboardGalaxyCategoryCount> ObjectCategories)
{
    /// <summary>Gets the empty breakdown (no templates, no categories).</summary>
    public static GalaxyObjectBreakdown Empty { get; } = new(
        Array.Empty<DashboardGalaxyTemplateUsage>(),
        Array.Empty<DashboardGalaxyCategoryCount>());
}
