using Shouldly;
using ZB.MOM.NatsNet.Server;

namespace ZB.MOM.NatsNet.Server.Tests.ImplBacklog;

public sealed partial class LeafNodeProxyTests
{
    [Fact] // T:1897
    public void LeafNodeHttpProxyConfigParsing_ShouldSucceed()
    {
        var errors = new List<Exception>();
        var warnings = new List<Exception>();

        var remotes = ServerOptions.ParseRemoteLeafNodes(
            new List<object?>
            {
                new Dictionary<string, object?>
                {
                    ["url"] = "ws://127.0.0.1:7422",
                    ["proxy"] = new Dictionary<string, object?>
                    {
                        ["url"] = "http://proxy.example.com:8080",
                        ["username"] = "user",
                        ["password"] = "pass",
                        ["timeout"] = "10s",
                    },
                },
            },
            errors,
            warnings);

        errors.ShouldBeEmpty();
        remotes.Count.ShouldBe(1);
        remotes[0].Proxy.Url.ShouldBe("http://proxy.example.com:8080");
        remotes[0].Proxy.Username.ShouldBe("user");
        remotes[0].Proxy.Password.ShouldBe("pass");
        remotes[0].Proxy.Timeout.ShouldBe(TimeSpan.FromSeconds(10));
    }

    [Fact] // T:1898
    public void LeafNodeHttpProxyConfigWarnings_ShouldSucceed()
    {
        var errors = new List<Exception>();
        var warnings = new List<Exception>();

        var cases = new[]
        {
            new Dictionary<string, object?>
            {
                ["url"] = "nats://127.0.0.1:7422",
                ["proxy"] = new Dictionary<string, object?>
                {
                    ["url"] = "http://proxy.example.com:8080",
                },
            },
            new Dictionary<string, object?>
            {
                ["urls"] = new List<object?> { "nats://127.0.0.1:7422", "ws://127.0.0.1:8080" },
                ["proxy"] = new Dictionary<string, object?>
                {
                    ["url"] = "http://proxy.example.com:8080",
                },
            },
            new Dictionary<string, object?>
            {
                ["url"] = "ws://127.0.0.1:7422",
                ["proxy"] = new Dictionary<string, object?>
                {
                    ["url"] = "http://proxy.example.com:8080",
                },
            },
        };

        foreach (var remote in cases)
        {
            var parsed = ServerOptions.ParseRemoteLeafNodes(new List<object?> { remote }, errors, warnings);
            parsed.Count.ShouldBe(1);
            parsed[0].Proxy.Url.ShouldBe("http://proxy.example.com:8080");
        }

        errors.ShouldBeEmpty();
    }
}
