// Reference: golang/nats-server/server/filestore.go
// Go uses S2/Snappy compression throughout FileStore:
//   - msgCompress / msgDecompress (filestore.go ~line 840)
//   - compressBlock / decompressBlock for block-level data
// These tests verify the .NET S2Codec helper used in the FSV2 envelope path.

using NATS.Server.JetStream.Storage;
using System.Text;

namespace NATS.Server.Tests.JetStream.Storage;

public sealed class S2CodecTests
{
    // Go: TestFileStoreBasics (S2 permutation) filestore_test.go:86
    [Fact]
    public void Compress_then_decompress_round_trips()
    {
        var original = "Hello, NATS JetStream S2 compression!"u8.ToArray();

        var compressed = S2Codec.Compress(original);
        var restored = S2Codec.Decompress(compressed);

        restored.ShouldBe(original);
    }

    [Fact]
    public void Compress_empty_returns_empty()
    {
        var compressed = S2Codec.Compress([]);
        compressed.ShouldBeEmpty();
    }

    [Fact]
    public void Decompress_empty_returns_empty()
    {
        var decompressed = S2Codec.Decompress([]);
        decompressed.ShouldBeEmpty();
    }

    [Fact]
    public void Compress_large_highly_compressible_payload()
    {
        // 1 MB of repeated 'A' — highly compressible.
        var original = new byte[1024 * 1024];
        Array.Fill(original, (byte)'A');

        var compressed = S2Codec.Compress(original);
        var restored = S2Codec.Decompress(compressed);

        // S2/Snappy should compress this well.
        compressed.Length.ShouldBeLessThan(original.Length);
        restored.ShouldBe(original);
    }

    [Fact]
    public void Compress_large_incompressible_payload_round_trips()
    {
        // 1 MB of random data — not compressible, but must still round-trip.
        var original = new byte[1024 * 1024];
        Random.Shared.NextBytes(original);

        var compressed = S2Codec.Compress(original);
        var restored = S2Codec.Decompress(compressed);

        restored.ShouldBe(original);
    }

    [Fact]
    public void Compress_single_byte_round_trips()
    {
        var original = new byte[] { 0x42 };
        var compressed = S2Codec.Compress(original);
        var restored = S2Codec.Decompress(compressed);
        restored.ShouldBe(original);
    }

    [Fact]
    public void Compress_binary_all_byte_values_round_trips()
    {
        var original = new byte[256];
        for (var i = 0; i < 256; i++)
            original[i] = (byte)i;

        var compressed = S2Codec.Compress(original);
        var restored = S2Codec.Decompress(compressed);
        restored.ShouldBe(original);
    }

    // Go: msgCompress with trailing CRC (filestore.go ~line 840) — the checksum
    // lives outside the S2 frame so only the body is compressed.
    [Fact]
    public void CompressWithTrailingChecksum_preserves_last_n_bytes_uncompressed()
    {
        const int checksumSize = 8;
        var body = Encoding.UTF8.GetBytes("NATS payload body that should be compressed");
        var checksum = new byte[checksumSize];
        Random.Shared.NextBytes(checksum);

        var input = body.Concat(checksum).ToArray();

        var result = S2Codec.CompressWithTrailingChecksum(input, checksumSize);

        // Last checksumSize bytes must be verbatim.
        var resultChecksum = result[^checksumSize..];
        resultChecksum.ShouldBe(checksum);
    }

    [Fact]
    public void CompressWithTrailingChecksum_zero_checksum_compresses_all()
    {
        var data = "Hello, no checksum"u8.ToArray();
        var result = S2Codec.CompressWithTrailingChecksum(data, 0);
        var restored = S2Codec.Decompress(result);
        restored.ShouldBe(data);
    }

    [Fact]
    public void DecompressWithTrailingChecksum_round_trips()
    {
        const int checksumSize = 8;
        var body = new byte[512];
        Random.Shared.NextBytes(body);
        var checksum = new byte[checksumSize];
        Random.Shared.NextBytes(checksum);

        var input = body.Concat(checksum).ToArray();

        var compressed = S2Codec.CompressWithTrailingChecksum(input, checksumSize);
        var restored = S2Codec.DecompressWithTrailingChecksum(compressed, checksumSize);

        restored.ShouldBe(input);
    }

    [Fact]
    public void CompressWithTrailingChecksum_empty_input_returns_empty()
    {
        var result = S2Codec.CompressWithTrailingChecksum([], 0);
        result.ShouldBeEmpty();
    }

    [Fact]
    public void CompressWithTrailingChecksum_negative_size_throws()
    {
        Should.Throw<ArgumentOutOfRangeException>(
            () => S2Codec.CompressWithTrailingChecksum([1, 2, 3], -1));
    }

    [Fact]
    public void DecompressWithTrailingChecksum_negative_size_throws()
    {
        Should.Throw<ArgumentOutOfRangeException>(
            () => S2Codec.DecompressWithTrailingChecksum([1, 2, 3], -1));
    }
}
