using ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums; // AlarmState

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment;

/// <summary>
/// A node in a Debug View composition tree. Attribute (and alarm)
/// names are path-qualified canonical names (e.g. <c>Motor1.Compressor.Pump</c>);
/// the tree is derived by splitting those names on <c>'.'</c>. A node is either a
/// branch (composition member, no payload, has children) or a leaf (one attribute
/// or one alarm). Consumed by the generic <c>TreeView&lt;TItem&gt;</c> component.
/// </summary>
public sealed class DebugTreeNode
{
    /// <summary>Full canonical path — stable TreeView key.</summary>
    public required string Key { get; init; }

    /// <summary>Display label (the last path segment).</summary>
    public required string Segment { get; init; }

    /// <summary>Child nodes, one per next path segment. Empty on a leaf.</summary>
    public List<DebugTreeNode> Children { get; } = new();

    // Leaf payloads — exactly one is set on a leaf; both null on a pure branch.
    /// <summary>The attribute value payload when this node is an attribute leaf; null otherwise.</summary>
    public AttributeValueChanged? Attribute { get; init; }
    /// <summary>The alarm state payload when this node is an alarm leaf (computed, native condition, or placeholder); null otherwise.</summary>
    public AlarmStateChanged? Alarm { get; init; }   // computed leaf, native condition, or placeholder

    /// <summary>True when this is a branch node grouping native alarm conditions.</summary>
    public bool IsNativeBinding { get; init; }       // branch grouping native conditions

    // Roll-up (set by the builder for branch nodes).
    /// <summary>The worst alarm state rolled up from this node's descendants.</summary>
    public AlarmState WorstState { get; set; } = AlarmState.Normal;
    /// <summary>The count of active alarms rolled up from this node's descendants.</summary>
    public int ActiveCount { get; set; }
    /// <summary>True when any descendant attribute has bad quality.</summary>
    public bool HasBadQuality { get; set; }

    /// <summary>True when this node is a branch (has one or more children).</summary>
    public bool HasChildren => Children.Count > 0;
}
