using ZB.MOM.WW.OtOpcUa.Configuration.Enums;

namespace ZB.MOM.WW.OtOpcUa.Configuration.Entities;

/// <summary>Physical OPC UA server node within a <see cref="ServerCluster"/>.</summary>
public sealed class ClusterNode
{
    /// <summary>Stable per-machine logical ID, e.g. "LINE3-OPCUA-A".</summary>
    public required string NodeId { get; set; }

    public required string ClusterId { get; set; }

    public required RedundancyRole RedundancyRole { get; set; }

    /// <summary>Machine hostname / IP.</summary>
    public required string Host { get; set; }

    public int OpcUaPort { get; set; } = 4840;

    public int DashboardPort { get; set; } = 8081;

    /// <summary>
    ///     OPC UA <c>ApplicationUri</c> — MUST be unique per node per OPC UA spec. Clients pin trust here.
    ///     Fleet-wide unique index enforces no two nodes share a value (decision #86).
    ///     Stored explicitly, NOT derived from <see cref="Host"/> at runtime — silent rewrite on
    ///     hostname change would break all client trust.
    /// </summary>
    public required string ApplicationUri { get; set; }

    /// <summary>Primary = 200, Secondary = 150 by default.</summary>
    public byte ServiceLevelBase { get; set; } = 200;

    /// <summary>
    ///     Per-node override JSON keyed by DriverInstanceId, merged onto cluster-level DriverConfig
    ///     at apply time. Minimal by intent (decision #81). Nullable when no overrides exist.
    /// </summary>
    public string? DriverConfigOverridesJson { get; set; }

    public bool Enabled { get; set; } = true;

    public DateTime? LastSeenAt { get; set; }

    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    public required string CreatedBy { get; set; }

    // Navigation
    public ServerCluster? Cluster { get; set; }
    public ICollection<ClusterNodeCredential> Credentials { get; set; } = [];
    public ClusterNodeGenerationState? GenerationState { get; set; }
}
