using NATS.Server.JetStream.Models;
using NATS.Server.JetStream.Storage;

namespace NATS.Server.JetStream.Tests;

public class StreamStoreContractTests
{
    [Fact]
    public async Task Append_increments_sequence_and_updates_state()
    {
        var store = new FakeStreamStore();
        var seq = await store.AppendAsync("foo", "bar"u8.ToArray(), default);

        seq.ShouldBe((ulong)1);
        (await store.GetStateAsync(default)).Messages.ShouldBe((ulong)1);
    }

    private sealed class FakeStreamStore : IStreamStore
    {
        private ulong _last;

        public ValueTask<ulong> AppendAsync(string subject, ReadOnlyMemory<byte> payload, CancellationToken ct)
        {
            _last++;
            return ValueTask.FromResult(_last);
        }

        public ValueTask<ApiStreamState> GetStateAsync(CancellationToken ct)
            => ValueTask.FromResult(new ApiStreamState { Messages = _last });

        public ValueTask<StoredMessage?> LoadAsync(ulong sequence, CancellationToken ct)
            => ValueTask.FromResult<StoredMessage?>(null);

        public ValueTask<StoredMessage?> LoadLastBySubjectAsync(string subject, CancellationToken ct)
            => ValueTask.FromResult<StoredMessage?>(null);

        public ValueTask<IReadOnlyList<StoredMessage>> ListAsync(CancellationToken ct)
            => ValueTask.FromResult<IReadOnlyList<StoredMessage>>([]);

        public ValueTask<bool> RemoveAsync(ulong sequence, CancellationToken ct)
            => ValueTask.FromResult(false);

        public ValueTask PurgeAsync(CancellationToken ct) => ValueTask.CompletedTask;

        public ValueTask<byte[]> CreateSnapshotAsync(CancellationToken ct)
            => ValueTask.FromResult(Array.Empty<byte>());

        public ValueTask RestoreSnapshotAsync(ReadOnlyMemory<byte> snapshot, CancellationToken ct)
            => ValueTask.CompletedTask;
    }
}
