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

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

public sealed class JetStreamVersioningTests
{
    [Fact] // T:1807
    public void JetStreamApiErrorOnRequiredApiLevelDirectGet_ShouldSucceed()
    {
        var goFile = "server/jetstream_versioning_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);

        }

        "JetStreamApiErrorOnRequiredApiLevelDirectGet_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamApiErrorOnRequiredApiLevelDirectGet".ShouldNotBeNullOrWhiteSpace();
    }

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

        }

        "JetStreamApiErrorOnRequiredApiLevelPullConsumerNextMsg_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamApiErrorOnRequiredApiLevelPullConsumerNextMsg".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:1804
    public void JetStreamMetadataStreamRestoreAndRestart_ShouldSucceed()
    {
        var cfg = new StreamConfig { Metadata = new Dictionary<string, string>() };
        var updated = JetStreamVersioning.SetDynamicStreamMetadata(cfg);
        var metadata = updated.Metadata!;

        metadata.ShouldContainKey(JetStreamVersioning.JsServerLevelMetadataKey);
        metadata.ShouldContainKey(JetStreamVersioning.JsServerVersionMetadataKey);

        JetStreamVersioning.DeleteDynamicMetadata(metadata);
        metadata.ShouldNotContainKey(JetStreamVersioning.JsServerLevelMetadataKey);
        metadata.ShouldNotContainKey(JetStreamVersioning.JsServerVersionMetadataKey);
    }

    [Fact] // T:1806
    public void JetStreamApiErrorOnRequiredApiLevel_ShouldSucceed()
    {
        var metadata = new Dictionary<string, string>
        {
            [JetStreamVersioning.JsRequiredLevelMetadataKey] = JetStreamVersioning.JsApiLevel.ToString(),
        };

        JetStreamVersioning.SupportsRequiredApiLevel(metadata).ShouldBeTrue();

        metadata[JetStreamVersioning.JsRequiredLevelMetadataKey] = "9999";
        JetStreamVersioning.SupportsRequiredApiLevel(metadata).ShouldBeFalse();
    }

}
