namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hosts;

using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;

/// <summary>
///     One configured host node within a cluster, as the <c>/hosts</c> page needs it: the cluster
///     it belongs to, its node id, and the host/port its OPC UA endpoint binds.
/// </summary>
/// <param name="ClusterId">Cluster this node belongs to.</param>
/// <param name="NodeId">Stable node identifier within the cluster.</param>
/// <param name="Host">Host name or address the node's OPC UA server binds.</param>
/// <param name="OpcUaPort">OPC UA TCP port the node listens on.</param>
public sealed record HostsNodeInfo(string ClusterId, string NodeId, string Host, int OpcUaPort);

/// <summary>
///     A configured driver instance's identity, used to enrich a live health snapshot with the
///     authored display name and driver type. Sourced from the config DB by the caller; this type
///     is deliberately DB-agnostic so the builder stays pure and unit-testable.
/// </summary>
/// <param name="DriverInstanceId">Globally-unique driver instance id (joins to a snapshot).</param>
/// <param name="ClusterId">Cluster the instance is configured under.</param>
/// <param name="Name">Authored display name.</param>
/// <param name="DriverType">Driver type string (Modbus, S7, Galaxy, ...).</param>
public sealed record HostsDriverInstanceInfo(string DriverInstanceId, string ClusterId, string Name, string DriverType);

/// <summary>
///     One row in the <c>/hosts</c> "Driver Instances" table: a live health snapshot enriched with
///     the configured name + type (both null when no matching instance is configured).
/// </summary>
/// <param name="DriverInstanceId">Globally-unique driver instance id.</param>
/// <param name="Name">Configured display name; null when unknown.</param>
/// <param name="DriverType">Configured driver type; null when unknown.</param>
/// <param name="State">Last-known driver state string (Healthy, Faulted, Reconnecting, ...).</param>
/// <param name="LastSuccessfulReadUtc">Most recent successful equipment read; null if never.</param>
/// <param name="LastError">Latest error message; null when none.</param>
/// <param name="ErrorCount5Min">Faulted-transition count in the last 5 minutes.</param>
/// <param name="PublishedUtc">Timestamp the snapshot was published.</param>
/// <param name="RediscoveryNeededUtc">When the driver last reported that the remote's tag set may have
///     changed; null when never (or when the driver cannot report it). Advisory — the served address space
///     is unchanged and an operator must re-browse the device via <c>/raw</c> to pick anything up.</param>
/// <param name="RediscoveryReason">The driver-supplied reason for that report; null when
///     <paramref name="RediscoveryNeededUtc"/> is null.</param>
public sealed record HostsDriverRow(
    string DriverInstanceId, string? Name, string? DriverType, string State,
    DateTime? LastSuccessfulReadUtc, string? LastError, int ErrorCount5Min, DateTime PublishedUtc,
    DateTime? RediscoveryNeededUtc = null, string? RediscoveryReason = null);

/// <summary>
///     One cluster's section on the <c>/hosts</c> page: its configured nodes plus its enriched
///     driver-instance rows.
/// </summary>
/// <param name="ClusterId">Cluster identifier.</param>
/// <param name="Nodes">Configured nodes in this cluster, ordered by node id.</param>
/// <param name="Drivers">Driver rows in this cluster, ordered by name then instance id.</param>
public sealed record HostsClusterGroup(string ClusterId, IReadOnlyList<HostsNodeInfo> Nodes, IReadOnlyList<HostsDriverRow> Drivers);

/// <summary>
///     Pure, DB-agnostic builder for the <c>/hosts</c> "Driver Instances" section. Groups live
///     driver-health snapshots by cluster, attaches each cluster's configured nodes, and enriches
///     every driver row with its configured name + type. Null-safe on all inputs and stable-ordered
///     so the Razor render is deterministic.
/// </summary>
public static class HostsDriverView
{
    /// <summary>
    ///     Builds the per-cluster view model. Clusters are the case-insensitive union of cluster ids
    ///     seen across <paramref name="snapshots"/> and <paramref name="nodes"/>, ordered
    ///     case-insensitively. Each group carries its nodes (ordered by node id) and its driver rows
    ///     (snapshots enriched by joining <paramref name="instances"/> on driver instance id;
    ///     unmatched ids yield null name/type), ordered by name-then-instance-id.
    /// </summary>
    /// <param name="snapshots">Live health snapshots; null treated as empty.</param>
    /// <param name="nodes">Configured cluster nodes; null treated as empty.</param>
    /// <param name="instances">Configured driver instances for enrichment; null treated as empty.</param>
    /// <returns>One <see cref="HostsClusterGroup"/> per cluster, ordered by cluster id.</returns>
    public static IReadOnlyList<HostsClusterGroup> Build(
        IEnumerable<DriverHealthChanged>? snapshots,
        IEnumerable<HostsNodeInfo>? nodes,
        IEnumerable<HostsDriverInstanceInfo>? instances)
    {
        var snapList = (snapshots ?? Enumerable.Empty<DriverHealthChanged>()).ToList();
        var nodeList = (nodes ?? Enumerable.Empty<HostsNodeInfo>()).ToList();
        var instanceList = instances ?? Enumerable.Empty<HostsDriverInstanceInfo>();

        // First-wins lookup keyed by driver instance id.
        var byInstance = new Dictionary<string, HostsDriverInstanceInfo>(StringComparer.OrdinalIgnoreCase);
        foreach (var instance in instanceList)
        {
            byInstance.TryAdd(instance.DriverInstanceId, instance);
        }

        var clusterIds = snapList.Select(s => s.ClusterId)
            .Concat(nodeList.Select(n => n.ClusterId))
            .Distinct(StringComparer.OrdinalIgnoreCase)
            .OrderBy(id => id, StringComparer.OrdinalIgnoreCase)
            .ToList();

        var groups = new List<HostsClusterGroup>(clusterIds.Count);
        foreach (var clusterId in clusterIds)
        {
            var clusterNodes = nodeList
                .Where(n => string.Equals(n.ClusterId, clusterId, StringComparison.OrdinalIgnoreCase))
                .OrderBy(n => n.NodeId, StringComparer.OrdinalIgnoreCase)
                .ToList();

            var clusterDrivers = snapList
                .Where(s => string.Equals(s.ClusterId, clusterId, StringComparison.OrdinalIgnoreCase))
                .Select(s =>
                {
                    byInstance.TryGetValue(s.DriverInstanceId, out var instance);
                    return new HostsDriverRow(
                        s.DriverInstanceId,
                        instance?.Name,
                        instance?.DriverType,
                        s.State,
                        s.LastSuccessfulReadUtc,
                        s.LastError,
                        s.ErrorCount5Min,
                        s.PublishedUtc,
                        s.RediscoveryNeededUtc,
                        s.RediscoveryReason);
                })
                .OrderBy(d => d.Name ?? d.DriverInstanceId, StringComparer.OrdinalIgnoreCase)
                .ThenBy(d => d.DriverInstanceId, StringComparer.OrdinalIgnoreCase)
                .ToList();

            groups.Add(new HostsClusterGroup(clusterId, clusterNodes, clusterDrivers));
        }

        return groups;
    }
}
