using Shouldly;
using Xunit;

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

/// <summary>
///     #139 connection-layer config knobs: keep-alive, idle-disconnect, reconnect backoff.
///     Coverage focuses on default behaviour (matches pre-#139 wire output exactly) and the
///     DTO-binding path so users can drive these from JSON without editing C#.
/// </summary>
[Trait("Category", "Unit")]
public sealed class ModbusConnectionOptionsTests
{
    /// <summary>Verifies that defaults match historical behaviour.</summary>
    [Fact]
    public void Defaults_Match_Historical_Behaviour()
    {
        var opts = new ModbusDriverOptions();

        opts.KeepAlive.Enabled.ShouldBeTrue();
        opts.KeepAlive.Time.ShouldBe(TimeSpan.FromSeconds(30));
        opts.KeepAlive.Interval.ShouldBe(TimeSpan.FromSeconds(10));
        opts.KeepAlive.RetryCount.ShouldBe(3);

        opts.IdleDisconnectTimeout.ShouldBeNull();

        opts.Reconnect.InitialDelay.ShouldBe(TimeSpan.Zero);
        opts.Reconnect.MaxDelay.ShouldBe(TimeSpan.FromSeconds(30));
        opts.Reconnect.BackoffMultiplier.ShouldBe(2.0);
    }

    /// <summary>Verifies that factory reads KeepAlive knobs from JSON.</summary>
    [Fact]
    public void Factory_Reads_KeepAlive_Knobs_From_Json()
    {
        const string json = """
        {
          "host": "10.0.0.10",
          "tags": [],
          "keepAlive": { "enabled": false, "timeMs": 60000, "intervalMs": 5000, "retryCount": 5 }
        }
        """;
        var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
        // Reach into options via reflection — the factory's options field is internal.
        var opts = (ModbusDriverOptions)typeof(ModbusDriver)
            .GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
            .GetValue(driver)!;

        opts.KeepAlive.Enabled.ShouldBeFalse();
        opts.KeepAlive.Time.ShouldBe(TimeSpan.FromMinutes(1));
        opts.KeepAlive.Interval.ShouldBe(TimeSpan.FromSeconds(5));
        opts.KeepAlive.RetryCount.ShouldBe(5);
    }

    /// <summary>Verifies that factory reads IdleDisconnect from JSON.</summary>
    [Fact]
    public void Factory_Reads_IdleDisconnect_From_Json()
    {
        const string json = """{ "host": "10.0.0.10", "tags": [], "idleDisconnectMs": 120000 }""";
        var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
        var opts = (ModbusDriverOptions)typeof(ModbusDriver)
            .GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
            .GetValue(driver)!;

        opts.IdleDisconnectTimeout.ShouldBe(TimeSpan.FromMinutes(2));
    }

    /// <summary>Verifies that factory reads Reconnect backoff from JSON.</summary>
    [Fact]
    public void Factory_Reads_Reconnect_Backoff_From_Json()
    {
        const string json = """
        {
          "host": "10.0.0.10",
          "tags": [],
          "reconnect": { "initialDelayMs": 500, "maxDelayMs": 60000, "backoffMultiplier": 1.5 }
        }
        """;
        var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
        var opts = (ModbusDriverOptions)typeof(ModbusDriver)
            .GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
            .GetValue(driver)!;

        opts.Reconnect.InitialDelay.ShouldBe(TimeSpan.FromMilliseconds(500));
        opts.Reconnect.MaxDelay.ShouldBe(TimeSpan.FromMinutes(1));
        opts.Reconnect.BackoffMultiplier.ShouldBe(1.5);
    }

    /// <summary>Verifies that factory with empty JSON uses all defaults.</summary>
    [Fact]
    public void Factory_With_Empty_Json_Uses_All_Defaults()
    {
        const string json = """{ "host": "10.0.0.10", "tags": [] }""";
        var driver = ModbusDriverFactoryExtensions.CreateInstance("modbus-1", json);
        var opts = (ModbusDriverOptions)typeof(ModbusDriver)
            .GetField("_options", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
            .GetValue(driver)!;

        // Every connection-layer field must match the historical defaults so existing config
        // rows stay bit-for-bit identical after #139.
        opts.KeepAlive.Enabled.ShouldBeTrue();
        opts.IdleDisconnectTimeout.ShouldBeNull();
        opts.Reconnect.InitialDelay.ShouldBe(TimeSpan.Zero);
        opts.AutoReconnect.ShouldBeTrue();
    }
}
