using ZB.MOM.WW.ScadaBridge.Commons.Serialization;
using ZB.MOM.WW.ScadaBridge.Commons.Types.DataConnections;

namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Types.DataConnections;

public class MxGatewayEndpointConfigSerializerTests
{
    [Fact]
    public void Serialize_then_Deserialize_round_trips_all_fields()
    {
        var original = new MxGatewayEndpointConfig
        {
            Endpoint = "https://gw:5001",
            ApiKey = "secret-key",
            ClientName = "client-a",
            WriteUserId = 7,
            UseTls = true,
            CaFile = "/certs/ca.pem",
            ServerName = "gw.local",
            ReadTimeoutMs = 1234
        };

        var json = MxGatewayEndpointConfigSerializer.Serialize(original);
        var round = MxGatewayEndpointConfigSerializer.Deserialize(json);

        Assert.Equal(original.Endpoint, round.Endpoint);
        Assert.Equal(original.ApiKey, round.ApiKey);
        Assert.Equal(original.ClientName, round.ClientName);
        Assert.Equal(original.WriteUserId, round.WriteUserId);
        Assert.Equal(original.UseTls, round.UseTls);
        Assert.Equal(original.CaFile, round.CaFile);
        Assert.Equal(original.ServerName, round.ServerName);
        Assert.Equal(original.ReadTimeoutMs, round.ReadTimeoutMs);
    }

    [Theory]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("   ")]
    [InlineData("{ not valid json")]
    public void Deserialize_null_blank_or_malformed_returns_default(string? json)
    {
        var def = new MxGatewayEndpointConfig();
        var result = MxGatewayEndpointConfigSerializer.Deserialize(json);
        Assert.Equal(def.Endpoint, result.Endpoint);
        Assert.Equal(def.ReadTimeoutMs, result.ReadTimeoutMs);
    }

    [Fact]
    public void ToFlatDict_FromFlatDict_round_trips()
    {
        var original = new MxGatewayEndpointConfig
        {
            Endpoint = "http://x:5000",
            ApiKey = "k",
            ClientName = "c",
            WriteUserId = 3,
            UseTls = true,
            CaFile = "/ca",
            ServerName = "s",
            ReadTimeoutMs = 999
        };

        var dict = MxGatewayEndpointConfigSerializer.ToFlatDict(original);
        var round = MxGatewayEndpointConfigSerializer.FromFlatDict(dict);

        Assert.Equal(original.Endpoint, round.Endpoint);
        Assert.Equal(original.ApiKey, round.ApiKey);
        Assert.Equal(original.ClientName, round.ClientName);
        Assert.Equal(original.WriteUserId, round.WriteUserId);
        Assert.Equal(original.UseTls, round.UseTls);
        Assert.Equal(original.CaFile, round.CaFile);
        Assert.Equal(original.ServerName, round.ServerName);
        Assert.Equal(original.ReadTimeoutMs, round.ReadTimeoutMs);
    }

    [Fact]
    public void FromFlatDict_invalid_numeric_falls_back_to_default()
    {
        var back = MxGatewayEndpointConfigSerializer.FromFlatDict(
            new Dictionary<string, string> { ["ReadTimeoutMs"] = "not-a-number" });
        Assert.Equal(new MxGatewayEndpointConfig().ReadTimeoutMs, back.ReadTimeoutMs);
    }
}
