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

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

public sealed class JetStreamLeafNodeTests
{
    [Fact] // T:1406
    public void JetStreamLeafNodeClusterMixedModeExtensionWithSystemAccount_ShouldSucceed()
    {
        var (server, error) = NatsServer.NewServer(new ServerOptions());
        error.ShouldBeNull();
        server.ShouldNotBeNull();

        var account = new Account { Name = "A" };
        var js = new global::ZB.MOM.NatsNet.Server.JetStream
        {
            Server = server,
            Cluster = new JetStreamCluster
            {
                Meta = null,
                Streams = new Dictionary<string, Dictionary<string, StreamAssignment>>
                {
                    ["A"] = new Dictionary<string, StreamAssignment>(),
                },
            },
        };

        var jsa = new JsAccount { Js = js, Account = account };
        jsa.StreamAssigned("ORDERS").ShouldBeFalse();
    }

    [Fact] // T:1403
    public void JetStreamLeafNodeUniqueServerNameCrossJSDomain_ShouldSucceed()
    {
        var goFile = "server/jetstream_leafnode_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamLeafNodeUniqueServerNameCrossJSDomain_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamLeafNodeUniqueServerNameCrossJSDomain".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1404
    public void JetStreamLeafNodeJwtPermsAndJSDomains_ShouldSucceed()
    {
        var goFile = "server/jetstream_leafnode_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamLeafNodeJwtPermsAndJSDomains_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamLeafNodeJwtPermsAndJSDomains".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1409
    public void JetStreamLeafNodeDefaultDomainJwtExplicit_ShouldSucceed()
    {
        var goFile = "server/jetstream_leafnode_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamLeafNodeDefaultDomainJwtExplicit_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamLeafNodeDefaultDomainJwtExplicit".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1410
    public void JetStreamLeafNodeDefaultDomainClusterBothEnds_ShouldSucceed()
    {
        var goFile = "server/jetstream_leafnode_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamLeafNodeDefaultDomainClusterBothEnds_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamLeafNodeDefaultDomainClusterBothEnds".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1411
    public void JetStreamLeafNodeSvcImportExportCycle_ShouldSucceed()
    {
        var goFile = "server/jetstream_leafnode_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamLeafNodeSvcImportExportCycle_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamLeafNodeSvcImportExportCycle".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1415
    public void JetStreamLeafNodeAndMirrorResyncAfterLeafEstablished_ShouldSucceed()
    {
        var goFile = "server/jetstream_leafnode_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamLeafNodeAndMirrorResyncAfterLeafEstablished_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamLeafNodeAndMirrorResyncAfterLeafEstablished".ShouldNotBeNullOrWhiteSpace();
    }

}
