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

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

public sealed partial class LeafNodeProxyTests
{
    [Fact] // T:1902
    public void LeafNodeHttpProxyTunnelBasic_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",
                        ["timeout"] = "2s",
                    },
                },
            },
            errors,
            warnings);

        errors.ShouldBeEmpty();
        remotes.Count.ShouldBe(1);
        remotes[0].Urls[0].Scheme.ShouldBe("ws");
        remotes[0].Proxy.Url.ShouldBe("http://proxy.example.com:8080");
        remotes[0].Proxy.Timeout.ShouldBe(TimeSpan.FromSeconds(2));
    }

    [Fact] // T:1903
    public void LeafNodeHttpProxyTunnelWithAuth_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"] = "testuser",
                        ["password"] = "testpass",
                        ["timeout"] = "5s",
                    },
                },
            },
            errors,
            warnings);

        errors.ShouldBeEmpty();
        remotes.Count.ShouldBe(1);
        remotes[0].Proxy.Url.ShouldBe("http://proxy.example.com:8080");
        remotes[0].Proxy.Username.ShouldBe("testuser");
        remotes[0].Proxy.Password.ShouldBe("testpass");
    }

    [Fact] // T:1899
    public void LeafNodeHttpProxyConnection_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",
                        ["timeout"] = "5s",
                    },
                },
            },
            errors,
            warnings);

        errors.ShouldBeEmpty();
        remotes.Count.ShouldBe(1);
        remotes[0].Urls.Count.ShouldBe(1);
        remotes[0].Urls[0].Scheme.ShouldBe("ws");
        remotes[0].Proxy.Url.ShouldBe("http://proxy.example.com:8080");
        remotes[0].Proxy.Timeout.ShouldBe(TimeSpan.FromSeconds(5));
    }

    [Fact] // T:1900
    public void LeafNodeHttpProxyWithAuthentication_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"] = "testuser",
                        ["password"] = "testpass",
                        ["timeout"] = "5s",
                    },
                },
            },
            errors,
            warnings);

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