using System.Text.Json;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient;

namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.IntegrationTests;

/// <summary>
///     Driver-side configuration + well-known opc-plc node identifiers that the smoke
///     tests address. Node IDs are stable across opc-plc releases — the simulator
///     guarantees the same <c>ns=3;s=...</c> names shipped since v1.0. If a release
///     bump breaks these, the fixture's pinned image tag needs a coordinated bump.
///     <para><b>These tests address the driver by RawPath, not by node id (fixed 2026-07-28).</b> v3
///     made the driver resolve every read/write/subscribe reference through its authored
///     <see cref="OpcUaClientDriverOptions.RawTags"/> table — a bare <c>ns=3;s=StepUp</c> is no longer a
///     reference the driver accepts, and one that does not resolve is failed LOCALLY as
///     <c>BadNodeIdInvalid</c> with no wire round-trip. The smoke tests still passed bare node ids, so all
///     three had been failing against a perfectly healthy simulator. Seeding <c>RawTags</c> here is what
///     makes them exercise the driver instead of its unresolved-reference guard.</para>
/// </summary>
public static class OpcPlcProfile
{
    /// <summary>opc-plc monotonically-increasing UInt32; ticks once per second under default opts.</summary>
    public const string StepUp = "ns=3;s=StepUp";

    /// <summary>opc-plc random Int32 node; new value ~every 100ms.</summary>
    public const string RandomSignedInt32 = "ns=3;s=RandomSignedInt32";

    /// <summary>opc-plc alternating boolean; flips every second.</summary>
    public const string AlternatingBoolean = "ns=3;s=AlternatingBoolean";

    /// <summary>opc-plc fast uint node — ticks every 100ms. Used for subscription-cadence tests.</summary>
    public const string FastUInt1 = "ns=3;s=FastUInt1";

    /// <summary>The RawPath the smoke tests address <see cref="StepUp"/> by.</summary>
    public const string StepUpRef = "Sim/OpcUaClient/plc/StepUp";

    /// <summary>The RawPath the smoke tests address <see cref="RandomSignedInt32"/> by.</summary>
    public const string RandomSignedInt32Ref = "Sim/OpcUaClient/plc/RandomSignedInt32";

    /// <summary>The RawPath the smoke tests address <see cref="AlternatingBoolean"/> by.</summary>
    public const string AlternatingBooleanRef = "Sim/OpcUaClient/plc/AlternatingBoolean";

    /// <summary>The RawPath the smoke tests address <see cref="FastUInt1"/> by.</summary>
    public const string FastUInt1Ref = "Sim/OpcUaClient/plc/FastUInt1";

    /// <summary>The authored raw-tag table the driver resolves the <c>*Ref</c> RawPaths through — the
    /// same <c>{"nodeId": …}</c> TagConfig shape the deploy artifact delivers.</summary>
    public static IReadOnlyList<RawTagEntry> RawTags { get; } =
    [
        Entry(StepUpRef, StepUp),
        Entry(RandomSignedInt32Ref, RandomSignedInt32),
        Entry(AlternatingBooleanRef, AlternatingBoolean),
        Entry(FastUInt1Ref, FastUInt1),
    ];

    private static RawTagEntry Entry(string rawPath, string nodeId) =>
        new(rawPath, JsonSerializer.Serialize(new { nodeId }), WriteIdempotent: true);

    /// <summary>Builds driver options for the OPC PLC endpoint.</summary>
    /// <param name="endpointUrl">The endpoint URL of the OPC PLC simulator.</param>
    /// <returns>Configured driver options for the OPC PLC.</returns>
    public static OpcUaClientDriverOptions BuildOptions(string endpointUrl) => new()
    {
        RawTags = RawTags,
        EndpointUrl = endpointUrl,
        SecurityPolicy = OpcUaSecurityPolicy.None,
        SecurityMode = OpcUaSecurityMode.None,
        AuthType = OpcUaAuthType.Anonymous,
        // opc-plc auto-accepts client certs (--aa) but we still present one; trust the
        // server's cert back since the simulator regenerates it each container spin-up
        // and there's no meaningful chain to validate against.
        AutoAcceptCertificates = true,
        Timeout = TimeSpan.FromSeconds(10),
        SessionTimeout = TimeSpan.FromSeconds(30),
        // opc-plc's standard address space mirrors verbatim — treat it as SystemPlatform so
        // §8 namespace validation passes without requiring a UNS mapping table.
        TargetNamespaceKind = OpcUaTargetNamespaceKind.SystemPlatform,
    };
}
