using System.Buffers;
using System.IO.Pipelines;
using System.Text;
using NATS.Server.Protocol;

namespace NATS.Server.Core.Tests;

public class ParserTests
{
    private static async Task<List<ParsedCommand>> ParseAsync(string input)
    {
        var pipe = new Pipe();
        var commands = new List<ParsedCommand>();

        // Write input to pipe
        var bytes = Encoding.ASCII.GetBytes(input);
        await pipe.Writer.WriteAsync(bytes);
        pipe.Writer.Complete();

        // Parse from pipe
        var parser = new NatsParser(maxPayload: NatsProtocol.MaxPayloadSize);
        while (true)
        {
            var result = await pipe.Reader.ReadAsync();
            var buffer = result.Buffer;

            while (parser.TryParse(ref buffer, out var cmd))
                commands.Add(cmd);

            pipe.Reader.AdvanceTo(buffer.Start, buffer.End);

            if (result.IsCompleted)
                break;
        }

        return commands;
    }

    [Fact]
    public async Task Parse_PING()
    {
        var cmds = await ParseAsync("PING\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Ping);
    }

    [Fact]
    public async Task Parse_PONG()
    {
        var cmds = await ParseAsync("PONG\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Pong);
    }

    [Fact]
    public async Task Parse_CONNECT()
    {
        var cmds = await ParseAsync("CONNECT {\"verbose\":false,\"echo\":true}\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Connect);
        Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldContain("verbose");
    }

    [Fact]
    public async Task Parse_SUB_without_queue()
    {
        var cmds = await ParseAsync("SUB foo 1\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Sub);
        cmds[0].Subject.ShouldBe("foo");
        cmds[0].Queue.ShouldBeNull();
        cmds[0].Sid.ShouldBe("1");
    }

    [Fact]
    public async Task Parse_SUB_with_queue()
    {
        var cmds = await ParseAsync("SUB foo workers 1\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Sub);
        cmds[0].Subject.ShouldBe("foo");
        cmds[0].Queue.ShouldBe("workers");
        cmds[0].Sid.ShouldBe("1");
    }

    [Fact]
    public async Task Parse_UNSUB()
    {
        var cmds = await ParseAsync("UNSUB 1\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Unsub);
        cmds[0].Sid.ShouldBe("1");
        cmds[0].MaxMessages.ShouldBe(-1);
    }

    [Fact]
    public async Task Parse_UNSUB_with_max()
    {
        var cmds = await ParseAsync("UNSUB 1 5\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Unsub);
        cmds[0].Sid.ShouldBe("1");
        cmds[0].MaxMessages.ShouldBe(5);
    }

    [Fact]
    public async Task Parse_PUB_with_payload()
    {
        var cmds = await ParseAsync("PUB foo 5\r\nHello\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Pub);
        cmds[0].Subject.ShouldBe("foo");
        cmds[0].ReplyTo.ShouldBeNull();
        Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe("Hello");
    }

    [Fact]
    public async Task Parse_PUB_with_reply()
    {
        var cmds = await ParseAsync("PUB foo reply 5\r\nHello\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Pub);
        cmds[0].Subject.ShouldBe("foo");
        cmds[0].ReplyTo.ShouldBe("reply");
        Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe("Hello");
    }

    [Fact]
    public async Task Parse_multiple_commands()
    {
        var cmds = await ParseAsync("PING\r\nPONG\r\nSUB foo 1\r\n");
        cmds.Count.ShouldBe(3);
        cmds[0].Type.ShouldBe(CommandType.Ping);
        cmds[1].Type.ShouldBe(CommandType.Pong);
        cmds[2].Type.ShouldBe(CommandType.Sub);
    }

    [Fact]
    public async Task Parse_PUB_zero_payload()
    {
        var cmds = await ParseAsync("PUB foo 0\r\n\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Pub);
        cmds[0].Payload.ToArray().ShouldBeEmpty();
    }

    [Fact]
    public async Task Parse_case_insensitive()
    {
        var cmds = await ParseAsync("ping\r\npub FOO 3\r\nabc\r\n");
        cmds.Count.ShouldBe(2);
        cmds[0].Type.ShouldBe(CommandType.Ping);
        cmds[1].Type.ShouldBe(CommandType.Pub);
    }

    [Fact]
    public async Task Parse_HPUB()
    {
        // HPUB subject 12 17\r\nNATS/1.0\r\n\r\nHello\r\n
        var header = "NATS/1.0\r\n\r\n";
        var payload = "Hello";
        var total = header.Length + payload.Length;
        var cmds = await ParseAsync($"HPUB foo {header.Length} {total}\r\n{header}{payload}\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.HPub);
        cmds[0].Subject.ShouldBe("foo");
        cmds[0].HeaderSize.ShouldBe(header.Length);
    }

    [Fact]
    public async Task Parse_INFO()
    {
        var cmds = await ParseAsync("INFO {\"server_id\":\"test\"}\r\n");
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Info);
    }

    // Mirrors Go TestParsePubArg: verifies subject, optional reply, and payload size
    // are parsed correctly across various combinations of spaces and tabs.
    // Reference: golang/nats-server/server/parser_test.go TestParsePubArg
    [Theory]
    [InlineData("PUB a 2\r\nok\r\n",           "a",   null,  "ok")]
    [InlineData("PUB foo 2\r\nok\r\n",          "foo", null,  "ok")]
    [InlineData("PUB foo  2\r\nok\r\n",         "foo", null,  "ok")]
    [InlineData("PUB  foo 2\r\nok\r\n",         "foo", null,  "ok")]
    [InlineData("PUB  foo  2\r\nok\r\n",        "foo", null,  "ok")]
    [InlineData("PUB foo bar 2\r\nok\r\n",      "foo", "bar", "ok")]
    [InlineData("PUB foo  bar  2\r\nok\r\n",    "foo", "bar", "ok")]
    [InlineData("PUB  foo bar 2\r\nok\r\n",     "foo", "bar", "ok")]
    [InlineData("PUB  foo  bar  2  \r\nok\r\n", "foo", "bar", "ok")]
    [InlineData("PUB a\t2\r\nok\r\n",           "a",   null,  "ok")]
    [InlineData("PUB foo\t2\r\nok\r\n",         "foo", null,  "ok")]
    [InlineData("PUB \tfoo\t2\r\nok\r\n",       "foo", null,  "ok")]
    [InlineData("PUB foo\t\t\t2\r\nok\r\n",     "foo", null,  "ok")]
    [InlineData("PUB foo\tbar\t2\r\nok\r\n",    "foo", "bar", "ok")]
    [InlineData("PUB foo\t\tbar\t\t2\r\nok\r\n","foo", "bar", "ok")]
    public async Task Parse_PUB_argument_variations(
        string input, string expectedSubject, string? expectedReply, string expectedPayload)
    {
        var cmds = await ParseAsync(input);
        cmds.ShouldHaveSingleItem();
        cmds[0].Type.ShouldBe(CommandType.Pub);
        cmds[0].Subject.ShouldBe(expectedSubject);
        cmds[0].ReplyTo.ShouldBe(expectedReply);
        Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe(expectedPayload);
    }

    // Helper that parses a protocol string and expects a ProtocolViolationException to be thrown.
    private static async Task<Exception> ParseExpectingErrorAsync(string input)
    {
        var pipe = new Pipe();
        var bytes = Encoding.ASCII.GetBytes(input);
        await pipe.Writer.WriteAsync(bytes);
        pipe.Writer.Complete();

        var parser = new NatsParser(maxPayload: NatsProtocol.MaxPayloadSize);
        Exception? caught = null;
        try
        {
            while (true)
            {
                var result = await pipe.Reader.ReadAsync();
                var buffer = result.Buffer;

                while (parser.TryParse(ref buffer, out _))
                {
                    // consume successfully parsed commands
                }

                pipe.Reader.AdvanceTo(buffer.Start, buffer.End);

                if (result.IsCompleted)
                    break;
            }
        }
        catch (Exception ex)
        {
            caught = ex;
        }

        caught.ShouldNotBeNull("Expected a ProtocolViolationException but no exception was thrown.");
        return caught!;
    }

    // Mirrors Go TestShouldFail: malformed protocol inputs that the parser must reject.
    // The .NET parser signals errors by throwing ProtocolViolationException.
    // Note: "PIx", "PINx" and "UNSUB_2" are not included here because the .NET parser
    // uses 2-byte prefix matching (b0+b1) rather than Go's byte-by-byte state machine.
    // As a result, "PIx" matches "PI"→PING and is silently accepted, and "UNSUB_2"
    // parses as UNSUB with sid "_2" — these are intentional behavioral differences.
    // Reference: golang/nats-server/server/parser_test.go TestShouldFail
    [Theory]
    [InlineData("Px\r\n")]
    [InlineData(" PING\r\n")]
    [InlineData("SUB\r\n")]
    [InlineData("SUB  \r\n")]
    [InlineData("SUB foo\r\n")]
    [InlineData("PUB foo\r\n")]
    [InlineData("PUB  \r\n")]
    [InlineData("PUB foo bar       \r\n")]
    public async Task Parse_malformed_protocol_fails(string input)
    {
        var ex = await ParseExpectingErrorAsync(input);
        ex.ShouldBeOfType<ProtocolViolationException>();
    }

    // Mirrors Go TestMaxControlLine: a control line exceeding 4096 bytes must be rejected.
    // Reference: golang/nats-server/server/parser_test.go TestMaxControlLine
    [Fact]
    public async Task Parse_exceeding_max_control_line_fails()
    {
        // Build a PUB command whose control line (subject + size field) exceeds 4096 bytes.
        var longSubject = new string('a', NatsProtocol.MaxControlLineSize);
        var input = $"PUB {longSubject} 0\r\n\r\n";
        var ex = await ParseExpectingErrorAsync(input);
        ex.ShouldBeOfType<ProtocolViolationException>();
    }

    // Mirrors Go TestParsePubSizeOverflow: oversized decimal payload lengths
    // must be rejected during PUB argument parsing.
    // Reference: golang/nats-server/server/parser_test.go TestParsePubSizeOverflow
    [Fact]
    public async Task Parse_pub_size_overflow_fails()
    {
        var ex = await ParseExpectingErrorAsync("PUB foo 1234567890\r\n");
        ex.ShouldBeOfType<ProtocolViolationException>();
        ex.Message.ShouldContain("Invalid payload size");
    }
}
