using NATS.Server.Configuration;

namespace NATS.Server.Gateways.Tests.Gateways;

public class GatewayRemoteConfigParityBatch3Tests
{
    [Fact]
    public void RemoteGatewayOptions_tracks_connection_attempts_and_implicit_flag()
    {
        var cfg = new RemoteGatewayOptions { Name = "GW-B", Implicit = true };

        cfg.IsImplicit().ShouldBeTrue();
        cfg.GetConnAttempts().ShouldBe(0);
        cfg.BumpConnAttempts().ShouldBe(1);
        cfg.BumpConnAttempts().ShouldBe(2);
        cfg.GetConnAttempts().ShouldBe(2);
        cfg.ResetConnAttempts();
        cfg.GetConnAttempts().ShouldBe(0);
    }

    [Fact]
    public void RemoteGatewayOptions_add_and_update_urls_normalize_and_deduplicate()
    {
        var cfg = new RemoteGatewayOptions();
        cfg.AddUrls(["127.0.0.1:7222", "nats://127.0.0.1:7222", "nats://127.0.0.1:7223"]);

        cfg.Urls.Count.ShouldBe(2);
        cfg.Urls.ShouldContain("nats://127.0.0.1:7222");
        cfg.Urls.ShouldContain("nats://127.0.0.1:7223");

        cfg.UpdateUrls(
            configuredUrls: ["127.0.0.1:7333"],
            discoveredUrls: ["nats://127.0.0.1:7334", "127.0.0.1:7333"]);

        cfg.Urls.Count.ShouldBe(2);
        cfg.Urls.ShouldContain("nats://127.0.0.1:7333");
        cfg.Urls.ShouldContain("nats://127.0.0.1:7334");
    }

    [Fact]
    public void RemoteGatewayOptions_save_tls_hostname_and_get_urls_helpers()
    {
        var cfg = new RemoteGatewayOptions
        {
            Urls = ["127.0.0.1:7444", "nats://localhost:7445"],
        };

        cfg.SaveTlsHostname("nats://gw.example.net:7522");
        cfg.TlsName.ShouldBe("gw.example.net");

        var urlStrings = cfg.GetUrlsAsStrings();
        urlStrings.Count.ShouldBe(2);
        urlStrings.ShouldContain("nats://127.0.0.1:7444");
        urlStrings.ShouldContain("nats://localhost:7445");

        var urls = cfg.GetUrls();
        urls.Count.ShouldBe(2);
        urls.ShouldContain(u => u.Authority == "127.0.0.1:7444");
        urls.ShouldContain(u => u.Authority == "localhost:7445");
    }
}
