using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;
using ZB.MOM.WW.OtOpcUa.Driver.FOCAS;
using ZB.MOM.WW.OtOpcUa.Driver.Modbus;
using ZB.MOM.WW.OtOpcUa.Driver.Mqtt;
using ZB.MOM.WW.OtOpcUa.Driver.S7;
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;

namespace ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;

/// <summary>
///     Deploy-time TagConfig inspection dispatched by <c>DriverType</c> (R2-11, 05/CONV-2). Mirrors the
///     AdminUI <c>TagConfigValidator</c> shape: a case-insensitive map from the canonical driver-type string
///     to that driver's lightweight <c>Contracts</c> parser <c>Inspect()</c>, returning human-readable
///     warnings (present-but-invalid enum values that the lenient runtime silently defaults, plus
///     structurally-unparseable TagConfig). Never throws.
///     <para><b>Coverage is every driver that ships an <c>Inspect</c>, and that invariant is guarded</b> by
///     <c>EquipmentTagConfigInspectorCoverageTests</c>, which reflects over the referenced
///     <c>*.Contracts</c> assemblies — so a driver that gains an inspector cannot be silently left out. This
///     is the same guard shape the driver-form dispatch maps use, and for the same reason: the previous
///     hand-maintained list had drifted (MQTT shipped an <c>Inspect</c> and was never added).</para>
///     <para><b>Residual gap, deliberately recorded rather than papered over.</b> Galaxy, OpcUaClient, Sql,
///     MTConnect and Calculation have <b>no</b> <c>Inspect</c> in a Contracts assembly, so this deploy-time
///     gate cannot check them — while the AdminUI's <c>TagConfigValidator</c> DOES cover all but Galaxy via
///     its <c>*TagConfigModel.Validate()</c> models, which live in the AdminUI project and cannot be
///     referenced from here. The asymmetry runs the wrong way: config authored through the modal is checked,
///     but config arriving by CSV import or API is not. Closing it means promoting those models (or an
///     equivalent parser) into each driver's Contracts assembly. Tracked as deferment-register G-7.</para>
/// </summary>
public static class EquipmentTagConfigInspector
{
    private static readonly IReadOnlyDictionary<string, Func<string, IReadOnlyList<string>>> Inspectors =
        new Dictionary<string, Func<string, IReadOnlyList<string>>>(StringComparer.OrdinalIgnoreCase)
        {
            [DriverTypeNames.Modbus]   = ModbusTagDefinitionFactory.Inspect,
            [DriverTypeNames.S7]       = S7TagDefinitionFactory.Inspect,
            [DriverTypeNames.AbCip]    = AbCipTagDefinitionFactory.Inspect,
            [DriverTypeNames.AbLegacy] = AbLegacyTagDefinitionFactory.Inspect,
            [DriverTypeNames.TwinCAT]  = TwinCATTagDefinitionFactory.Inspect,
            [DriverTypeNames.FOCAS]    = FocasTagDefinitionFactory.Inspect,
            [DriverTypeNames.Mqtt]     = MqttTagDefinitionFactory.Inspect,
        };

    /// <summary>Inspects a tag's <c>TagConfig</c> for the given driver type. Returns the driver's warnings,
    /// or an empty list when the driver type has no inspector (see the class remarks) or the inputs are
    /// null.</summary>
    /// <param name="driverType">The bound driver's <c>DriverType</c> (canonical string, case-insensitive).</param>
    /// <param name="tagConfig">The equipment tag's <c>TagConfig</c> JSON.</param>
    /// <returns>The warnings; empty when the driver is unmapped or clean.</returns>
    public static IReadOnlyList<string> Inspect(string? driverType, string? tagConfig)
    {
        if (string.IsNullOrEmpty(driverType) || tagConfig is null) return Array.Empty<string>();
        return Inspectors.TryGetValue(driverType, out var inspect) ? inspect(tagConfig) : Array.Empty<string>();
    }

    /// <summary>Whether the given driver type has a mapped inspector.</summary>
    /// <param name="driverType">The bound driver's <c>DriverType</c>.</param>
    /// <returns><see langword="true"/> when an inspector is registered for the driver type.</returns>
    public static bool IsMapped(string? driverType) =>
        !string.IsNullOrEmpty(driverType) && Inspectors.ContainsKey(driverType);

    /// <summary>The driver types this gate can check — the guard test's read of the map.</summary>
    public static IReadOnlyCollection<string> MappedDriverTypes => (IReadOnlyCollection<string>)Inspectors.Keys;
}
