using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.Logging.Abstractions;
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Server;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;

namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests;

/// <summary>
/// Source plan Task 60 — closes the audit gap. Boots two real <see cref="StandardServer"/>
/// instances on loopback, each configured with the other's <c>ApplicationUri</c> in
/// <see cref="OpcUaApplicationHostOptions.PeerApplicationUris"/>. A real OPC UA client connects
/// to Node A, reads <c>Server.ServerArray</c>, and asserts both URIs are visible — the
/// warm-redundancy discovery contract clients depend on.
/// </summary>
public sealed class DualEndpointTests
{
    private const string NodeAUri = "urn:OtOpcUa.DualEndpoint.NodeA";
    private const string NodeBUri = "urn:OtOpcUa.DualEndpoint.NodeB";

    /// <summary>Verifies that a client can read the ServerArray containing both redundant node URIs.</summary>
    [Fact]
    public async Task Client_reads_both_ApplicationUris_from_NodeA_ServerArray()
    {
        var pkiRootA = Path.Combine(Path.GetTempPath(), $"otopcua-pki-a-{Guid.NewGuid():N}");
        var pkiRootB = Path.Combine(Path.GetTempPath(), $"otopcua-pki-b-{Guid.NewGuid():N}");
        var portA = AllocateFreePort();
        var portB = AllocateFreePort();

        try
        {
            await using var nodeA = await StartNodeAsync(NodeAUri, portA, pkiRootA, peers: new[] { NodeBUri });
            await using var nodeB = await StartNodeAsync(NodeBUri, portB, pkiRootB, peers: new[] { NodeAUri });

            var serverArray = await ReadServerArrayAsync($"opc.tcp://127.0.0.1:{portA}/OtOpcUa");
            serverArray.ShouldContain(NodeAUri);
            serverArray.ShouldContain(NodeBUri);
        }
        finally
        {
            if (Directory.Exists(pkiRootA)) Directory.Delete(pkiRootA, recursive: true);
            if (Directory.Exists(pkiRootB)) Directory.Delete(pkiRootB, recursive: true);
        }
    }

    private static async Task<OpcUaApplicationHost> StartNodeAsync(
        string applicationUri, int port, string pkiRoot, string[] peers)
    {
        var options = new OpcUaApplicationHostOptions
        {
            ApplicationName = applicationUri,
            ApplicationUri = applicationUri,
            OpcUaPort = port,
            PublicHostname = "127.0.0.1",
            PkiStoreRoot = pkiRoot,
            EnabledSecurityProfiles = new List<OpcUaSecurityProfile> { OpcUaSecurityProfile.None },
            AutoAcceptUntrustedClientCertificates = true,
            PeerApplicationUris = peers,
        };
        var server = new StandardServer();
        var host = new OpcUaApplicationHost(options, NullLogger<OpcUaApplicationHost>.Instance);
        await host.StartAsync(server, CancellationToken.None);
        return host;
    }

    private static async Task<string[]> ReadServerArrayAsync(string endpointUrl)
    {
        // SDK 1.5.374 sync-style session-open path — mirrors src/Client/.../DefaultSessionFactory.cs
        // and DefaultApplicationConfigurationFactory.cs. The 1.5.378 telemetry/async overloads are
        // not available at this pinned version.
        var clientPki = TestClientSecurity.AllocatePkiRoot();
        try
        {
            var appConfig = new ApplicationConfiguration
            {
                ApplicationName = "OtOpcUa.DualEndpointClient",
                ApplicationUri = $"urn:OtOpcUa.DualEndpointClient.{Guid.NewGuid():N}",
                ApplicationType = ApplicationType.Client,
                SecurityConfiguration = TestClientSecurity.Build(clientPki),
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60_000 },
            };
            await appConfig.ValidateAsync(ApplicationType.Client, default);
            appConfig.CertificateValidator.CertificateValidation += (_, e) => e.Accept = true;

            // 1.5.378: the discovery/session/read client surface moved to async.
            var endpoint = await CoreClientUtils.SelectEndpointAsync(
                appConfig, endpointUrl, false, DefaultTelemetry.Create(_ => { }), default);
            var endpointConfiguration = EndpointConfiguration.Create(appConfig);
            var configuredEndpoint = new ConfiguredEndpoint(null, endpoint, endpointConfiguration);

            using var session = await new DefaultSessionFactory(DefaultTelemetry.Create(_ => { })).CreateAsync(
                appConfig,
                configuredEndpoint,
                updateBeforeConnect: false,
                checkDomain: false,
                sessionName: "DualEndpointTests",
                sessionTimeout: 60_000,
                identity: new UserIdentity(new AnonymousIdentityToken()),
                preferredLocales: null,
                ct: default);

            var value = await session.ReadValueAsync(VariableIds.Server_ServerArray, default);
            return (string[])value.Value;
        }
        finally
        {
            if (Directory.Exists(clientPki)) Directory.Delete(clientPki, recursive: true);
        }
    }

    private static int AllocateFreePort()
    {
        var listener = new TcpListener(IPAddress.Loopback, 0);
        listener.Start();
        var port = ((IPEndPoint)listener.LocalEndpoint).Port;
        listener.Stop();
        return port;
    }
}
