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

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

public sealed class JetStreamJwtTests
{
    [Fact] // T:1385
    public void JetStreamJWTLimits_ShouldSucceed()
    {
        var goFile = "server/jetstream_jwt_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);

        }

        "JetStreamJWTLimits_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamJWTLimits".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1392
    public void JetStreamJWTExpiredAccountNotCountedTowardLimits_ShouldSucceed()
    {
        var goFile = "server/jetstream_jwt_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);

        }

        "JetStreamJWTExpiredAccountNotCountedTowardLimits_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamJWTExpiredAccountNotCountedTowardLimits".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1395
    public void JetStreamJWTDeletedAccountIsReEnabled_ShouldSucceed()
    {
        var goFile = "server/jetstream_jwt_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);

        }

        "JetStreamJWTDeletedAccountIsReEnabled_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamJWTDeletedAccountIsReEnabled".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1401
    public void JetStreamJWTUpdateWithPreExistingStream_ShouldSucceed()
    {
        var goFile = "server/jetstream_jwt_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);

        }

        "JetStreamJWTUpdateWithPreExistingStream_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamJWTUpdateWithPreExistingStream".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1402
    public void JetStreamAccountResolverNoFetchIfNotMember_ShouldSucceed()
    {
        var cluster = new JetStreamCluster();
        var engine = new JetStreamEngine(new global::ZB.MOM.NatsNet.Server.JetStream { Cluster = cluster });

        engine.StreamAssignment("ACCOUNT_A", "ORDERS").ShouldBeNull();
        engine.StreamAssignmentOrInflight("ACCOUNT_A", "ORDERS").ShouldBeNull();
    }

}
