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

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

public sealed class JetStreamFileStoreTests
{
    [Fact] // T:575
    public void JetStreamFileStoreSubjectsRemovedAfterSecureErase_ShouldSucceed()
    {
        var root = Path.Combine(Path.GetTempPath(), $"impl-fs-{Guid.NewGuid():N}");
        Directory.CreateDirectory(root);
        JetStreamFileStore? fs = null;

        try
        {
            fs = new JetStreamFileStore(
                new FileStoreConfig { StoreDir = root },
                new FileStreamInfo
                {
                    Created = DateTime.UtcNow,
                    Config = new StreamConfig
                    {
                        Name = "TEST",
                        Storage = StorageType.FileStorage,
                        Subjects = ["test.*"],
                    },
                });

            fs.StoreMsg("test.1", null, "msg1"u8.ToArray(), 0).Seq.ShouldBe(1UL);
            fs.StoreMsg("test.2", null, "msg2"u8.ToArray(), 0).Seq.ShouldBe(2UL);
            fs.StoreMsg("test.3", null, "msg3"u8.ToArray(), 0).Seq.ShouldBe(3UL);

            var before = fs.SubjectsTotals(">");
            before.Count.ShouldBe(3);
            before.ShouldContainKey("test.1");
            before.ShouldContainKey("test.2");
            before.ShouldContainKey("test.3");

            var (removed, err) = fs.EraseMsg(1);
            removed.ShouldBeTrue();
            err.ShouldBeNull();

            var after = fs.SubjectsTotals(">");
            after.Count.ShouldBe(2);
            after.ContainsKey("test.1").ShouldBeFalse();
            after["test.2"].ShouldBe(1UL);
            after["test.3"].ShouldBe(1UL);
        }
        finally
        {
            fs?.Stop();
            Directory.Delete(root, recursive: true);
        }
    }

}
