using ZB.MOM.WW.MxGateway.Contracts.Proto.Galaxy;
using ZB.MOM.WW.MxGateway.Server.Dashboard;

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

/// <summary>
///     Immutable snapshot of the Galaxy Repository browse data held by
///     <see cref="GalaxyHierarchyCache"/>. Multiple gRPC clients share the same
///     materialized object list and precomputed dashboard projection.
/// </summary>
public sealed record GalaxyHierarchyCacheEntry(
    GalaxyCacheStatus Status,
    long Sequence,
    DateTimeOffset? LastQueriedAt,
    DateTimeOffset? LastSuccessAt,
    DateTimeOffset? LastDeployTime,
    string? LastError,
    IReadOnlyList<GalaxyObject> Objects,
    GalaxyHierarchyIndex Index,
    DashboardGalaxySummary DashboardSummary,
    int ObjectCount,
    int AreaCount,
    int AttributeCount,
    int HistorizedAttributeCount,
    int AlarmAttributeCount)
{
    /// <summary>Gets an empty Galaxy hierarchy cache entry.</summary>
    public static GalaxyHierarchyCacheEntry Empty { get; } = new(
        Status: GalaxyCacheStatus.Unknown,
        Sequence: 0,
        LastQueriedAt: null,
        LastSuccessAt: null,
        LastDeployTime: null,
        LastError: null,
        Objects: Array.Empty<GalaxyObject>(),
        Index: GalaxyHierarchyIndex.Empty,
        DashboardSummary: DashboardGalaxySummary.Unknown,
        ObjectCount: 0,
        AreaCount: 0,
        AttributeCount: 0,
        HistorizedAttributeCount: 0,
        AlarmAttributeCount: 0);

    /// <summary>Gets a value indicating whether the cache entry contains usable data.</summary>
    public bool HasData => Status is GalaxyCacheStatus.Healthy or GalaxyCacheStatus.Stale;
}
