using System.Net.Sockets;

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

/// <summary>
///     Reachability probe for a Modbus TCP simulator (pymodbus in Docker, see
///     <c>Docker/docker-compose.yml</c>) or a real PLC. Parses
///     <c>MODBUS_SIM_ENDPOINT</c> (default <c>10.100.0.35:5020</c> — the shared Docker host) and TCP-connects once at
///     fixture construction. Each test checks <see cref="SkipReason"/> and calls
///     <c>Assert.Skip</c> when the endpoint was unreachable, so a dev box without a running
///     simulator still passes `dotnet test` cleanly — matches the Galaxy live-smoke pattern in
///     <c>GalaxyRepositoryLiveSmokeTests</c>.
/// </summary>
/// <remarks>
///     <para>
///         Do NOT keep the probe socket open for the life of the fixture. The probe is a
///         one-shot liveness check; tests open their own transports (the real
///         <see cref="ModbusTcpTransport"/>) against the same endpoint. Sharing a socket
///         across tests would serialize them on a single TCP stream.
///     </para>
///     <para>
///         The fixture is a collection fixture so the reachability probe runs once per test
///         session, not per test — checking every test would waste several seconds against a
///         firewalled endpoint that times out each attempt.
///     </para>
/// </remarks>
public sealed class ModbusSimulatorFixture : IAsyncDisposable
{
    // PR 43: default port is 5020 (pymodbus convention) instead of 502 (Modbus standard).
    // Picking 5020 sidesteps the privileged-port admin requirement on Windows + matches the
    // port baked into the pymodbus simulator JSON profiles in Docker/profiles/. Override with
    // MODBUS_SIM_ENDPOINT to point at a real PLC on its native port 502, or to a
    // locally-running container if the shared host is unavailable.
    // 10.100.0.35 = the shared Docker host (see CLAUDE.md "Docker Workflow"). Migrated
    // off this VM's localhost on 2026-04-28 alongside the rest of the Docker-host move.
    private const string DefaultEndpoint = "10.100.0.35:5020";
    private const string EndpointEnvVar = "MODBUS_SIM_ENDPOINT";

    /// <summary>Gets the host address of the Modbus simulator.</summary>
    public string Host { get; }

    /// <summary>Gets the port of the Modbus simulator.</summary>
    public int Port { get; }

    /// <summary>Gets the skip reason if the simulator is unreachable; otherwise null.</summary>
    public string? SkipReason { get; }

    /// <summary>Initializes a new instance of the <see cref="ModbusSimulatorFixture"/> class.</summary>
    public ModbusSimulatorFixture()
    {
        var raw = Environment.GetEnvironmentVariable(EndpointEnvVar) ?? DefaultEndpoint;
        var parts = raw.Split(':', 2);
        Host = parts[0];
        Port = parts.Length == 2 && int.TryParse(parts[1], out var p) ? p : 502;

        try
        {
            // Force IPv4 family on the probe — pymodbus's TCP server binds 0.0.0.0 (IPv4 only)
            // while .NET's TcpClient default-resolves "localhost" → IPv6 ::1 first, fails to
            // connect, and only then tries IPv4. Under .NET 10 the IPv6 fail surfaces as a
            // 2s timeout (no graceful fallback by default), so the C# probe times out even
            // though a PowerShell probe of the same endpoint succeeds. Resolving + dialing
            // explicit IPv4 sidesteps the dual-stack ordering.
            using var client = new TcpClient(System.Net.Sockets.AddressFamily.InterNetwork);
            var task = client.ConnectAsync(
                System.Net.Dns.GetHostAddresses(Host)
                    .FirstOrDefault(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    ?? System.Net.IPAddress.Loopback,
                Port);
            if (!task.Wait(TimeSpan.FromSeconds(2)) || !client.Connected)
            {
                SkipReason = $"Modbus simulator at {Host}:{Port} did not accept a TCP connection within 2s. " +
                             $"Start the pymodbus Docker container (docker compose -f Docker/docker-compose.yml --profile standard up -d) " +
                             $"or override {EndpointEnvVar}, then re-run.";
            }
        }
        catch (Exception ex)
        {
            SkipReason = $"Modbus simulator at {Host}:{Port} unreachable: {ex.GetType().Name}: {ex.Message}. " +
                         $"Start the pymodbus Docker container (docker compose -f Docker/docker-compose.yml --profile standard up -d) " +
                         $"or override {EndpointEnvVar}, then re-run.";
        }
    }

    /// <inheritdoc />
    public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}

[Xunit.CollectionDefinition(Name)]
public sealed class ModbusSimulatorCollection : Xunit.ICollectionFixture<ModbusSimulatorFixture>
{
    public const string Name = "ModbusSimulator";
}
