// Copyright © WireMock.Net

using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json.Serialization;
using JsonConverter.System.Text.Json;
using WireMock.Matchers;
using WireMock.Net.Xunit;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;

namespace WireMock.Net.Tests;

public partial class WireMockServerTests
{
    public class DummyClass
    {
        public string? Hi { get; set; }
    }

    public class Person
    {
        [JsonPropertyName("first_name")]
        public string FirstName { get; set; } = string.Empty;

        [JsonPropertyName("last_name")]
        public string LastName { get; set; } = string.Empty;
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_UsingWireMockServerSettings_SystemTextJsonConverter_ShouldReturnCorrectResponse()
    {
        // Arange
        var person = new Person { FirstName = "John", LastName = "Smith" };

        using var server = WireMockServer.Start(new WireMockServerSettings
        {
            DefaultJsonSerializer = new SystemTextJsonConverter()
        });

        // Act
        server
            .Given(Request.Create().UsingAnyMethod())
            .RespondWith(Response.Create().WithBodyAsJson(person));

        var response = await server.CreateClient().GetStringAsync("/", _ct);

        // Assert
        response.Should().BeEquivalentTo("{\"first_name\":\"John\",\"last_name\":\"Smith\"}");
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_WithJsonPartialWildcardMatcher()
    {
        // Arrange
        using var server = WireMockServer.Start(new WireMockServerSettings
        {
            Logger = new TestOutputHelperWireMockLogger(testOutputHelper)
        });
        server
            .Given(
                Request.Create()
                    .WithPath("/test")
                    .UsingPost()
                    .WithBody(new JsonPartialWildcardMatcher(
                        new
                        {
                            date = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$",
                            id = 1
                        },
                        ignoreCase: true,
                        regex: true
                    )
                )
            )
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("matched"));

        // Act
        var requestUri = new Uri($"http://localhost:{server.Port}/test");

        var response = await server.CreateClient().PostAsync(requestUri, new StringContent("{\"date\":\"2026-06-09T22:23:18.53421+00:00\",\"id\":1}"), _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_WithSystemTextJsonPartialWildcardMatcher()
    {
        // Arrange
        using var server = WireMockServer.Start(new WireMockServerSettings
        {
            Logger = new TestOutputHelperWireMockLogger(testOutputHelper),
            DefaultJsonSerializer = new SystemTextJsonConverter()
        });
        server
            .Given(
                Request.Create()
                    .WithPath("/test")
                    .UsingPost()
                    .WithBody(new SystemTextJsonPartialWildcardMatcher(
                        new
                        {
                            date = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$",
                            id = 1
                        },
                        ignoreCase: true,
                        regex: true
                    )
                )
            )
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("matched"));

        // Act
        var requestUri = new Uri($"http://localhost:{server.Port}/test");

        var response = await server.CreateClient().PostAsync(requestUri, new StringContent("{\"date\":\"2026-06-09T22:23:18.53421+00:00\",\"id\":1}"), _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server.Given(
            Request.Create()
                .WithPath("/a")
                .WithBody(
                    [
                        new JmesPathMatcher("requestId == '1'"),
                        new JmesPathMatcher("value == 'A'")
                    ],
                    MatchOperator.And
                )
                .UsingPost()
            )
            .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK));

        server.Given(
                Request.Create()
                    .WithPath("/a")
                    .WithBody(
                        [
                            new JmesPathMatcher("requestId == '2'"),
                            new JmesPathMatcher("value == 'A'")
                        ],
                        MatchOperator.And
                    )
                    .UsingPost()
            )
            .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.Moved));

        // Act
        var requestUri = new Uri($"http://localhost:{server.Port}/a");

        var json = new { requestId = "1", value = "A" };
        var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server.Given(
                Request.Create()
                    .WithPath("/a")
                    .WithBody(
                        [
                            new JmesPathMatcher("extra == 'X'"),
                            new JmesPathMatcher("requestId == '1'"),
                            new JmesPathMatcher("value == 'A'")
                        ],
                        MatchOperator.And
                    )
                    .UsingPost()
            )
            .AtPriority(1) // Higher priority
            .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK));

        server.Given(
                Request.Create()
                    .WithPath("/a")
                    .WithBody(
                        [
                            new JmesPathMatcher("requestId == '1'"),
                            new JmesPathMatcher("value == 'A'")
                        ],
                        MatchOperator.And
                    )
                    .UsingPost()
            )
            .AtPriority(2)
            .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.Moved));

        // Act
        var requestUri = new Uri($"http://localhost:{server.Port}/a");

        var json = new { extra = "X", requestId = "1", value = "A" };
        var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server.Given(
                Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
            )
            .RespondWith(
                Response.Create().WithStatusCode(200)
            );

        var jsonObject = new DummyClass
        {
            Hi = "Hello World!"
        };

        // Act
        var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject, _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server.Given(
                Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
            )
            .RespondWith(
                Response.Create().WithStatusCode(200)
            );

        // Act
        var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"), _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_ShouldMatch()
    {
        // Arrange
        using var server = WireMockServer.Start();

        var matcher = new JsonPartialWildcardMatcher(new { method = "initialize", id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
        server.Given(Request.Create()
            .WithHeader("Content-Type", "application/json*")
            .UsingPost()
            .WithPath("/foo")
            .WithBody(matcher)
        )
        .RespondWith(Response.Create()
            .WithHeader("Content-Type", "application/json")
            .WithBody("""
                      {"jsonrpc":"2.0","id":"{{request.bodyAsJson.id}}","result":{"protocolVersion":"2024-11-05","capabilities":{"logging":{},"prompts":{"listChanged":true},"resources":{"subscribe":true,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"ExampleServer","version":"1.0.0"}}}
                      """)
            .WithStatusCode(200)
            .WithTransformer(true)
        );

        // Act
        var content = "{\"jsonrpc\":\"2.0\",\"id\":\"ec475f56d4694b48bc737500ba575b35-1\",\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"GitHub Test\",\"version\":\"1.0.0\"}}}";
        var response = await new HttpClient()
            .PostAsync($"{server.Url}/foo", new StringContent(content, Encoding.UTF8, "application/json"), _ct)
;

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        var responseText = await response.Content.ReadAsStringAsync(_ct);
        responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
    }

#if NET6_0_OR_GREATER
    [Fact]
    public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
    {
        // Arrange
        using var server = WireMockServer.Start(settings =>
        {
            settings.Logger = new TestOutputHelperWireMockLogger(testOutputHelper);
            settings.DefaultJsonSerializer = new SystemTextJsonConverter();
        });

        var matcher = new SystemTextJsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
        server.Given(Request.Create()
            .WithHeader("Content-Type", "application/json*")
            .UsingPost()
            .WithPath("/system-text-json")
            .WithBody(matcher)
        )
        .RespondWith(Response.Create()
            .WithBody("OK")
        );

        // Act
        var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
        using var httpClient = new HttpClient();
        var response = await httpClient.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"), _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        var responseText = await response.Content.ReadAsStringAsync(_ct);
        responseText.Should().Contain("OK");
    }
#endif

    [Fact]
    public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server.Given(
            Request.Create()
                .UsingPost()
                .WithPath("/foo")
                .WithBody(values => values != null && values["key1"] == "value1")
            )
            .RespondWith(
                Response.Create()
            );

        // Act
        var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
        var response = await new HttpClient()
            .PostAsync($"{server.Url}/foo", content, _ct);

        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server.Given(
                Request.Create()
                    .UsingPost()
                    .WithPath("/foo")
                    .WithHeader("Content-Type", "application/x-www-form-urlencoded")
                    .WithBody(new ExactMatcher("name=John+Doe&email=johndoe%40example.com")
                )
            )
            .RespondWith(
                Response.Create()
            );

        // Act
        var content = new FormUrlEncodedContent(
        [
            new KeyValuePair<string, string>("name", "John Doe"),
            new KeyValuePair<string, string>("email", "johndoe@example.com")
        ]);
        using var httpClient = new HttpClient();
        var response = await httpClient.PostAsync($"{server.Url}/foo", content, _ct)
;
        // Assert
        response.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFormUrlEncodedMatcher()
    {
        // Arrange
        var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
        using var server = WireMockServer.Start();
        server.Given(
            Request.Create()
                .UsingPost()
                .WithPath("/foo")
                .WithHeader("Content-Type", "application/x-www-form-urlencoded")
                .WithBody(matcher)
            )
            .RespondWith(
                Response.Create()
            );

        server.Given(
            Request.Create()
                .UsingPost()
                .WithPath("/bar")
                .WithHeader("Content-Type", "application/x-www-form-urlencoded")
                .WithBody(matcher)
            )
            .RespondWith(
                Response.Create()
            );

        // Act 1
        var contentOrdered = new FormUrlEncodedContent(
        [
            new KeyValuePair<string, string>("name", "John Doe"),
            new KeyValuePair<string, string>("email", "johndoe@example.com")
        ]);
        var responseOrdered = await new HttpClient()
            .PostAsync($"{server.Url}/foo", contentOrdered, _ct)
;

        // Assert 1
        responseOrdered.StatusCode.Should().Be(HttpStatusCode.OK);


        // Act 2
        var contentUnordered = new FormUrlEncodedContent(
        [
            new KeyValuePair<string, string>("email", "johndoe@example.com"),
            new KeyValuePair<string, string>("name", "John Doe"),
        ]);
        var responseUnordered = await new HttpClient()
            .PostAsync($"{server.Url}/bar", contentUnordered, _ct)
;

        // Assert 2
        responseUnordered.StatusCode.Should().Be(HttpStatusCode.OK);

        server.Stop();
    }

    [Fact]
    public async Task WireMockServer_WithSseBody()
    {
        // Arrange
        using var server = WireMockServer.Start();
        server
            .WhenRequest(r => r
                .UsingGet()
                .WithPath("/sse")
            )
            .ThenRespondWith(r => r
                .WithHeader("Content-Type", "text/event-stream")
                .WithHeader("Cache-Control", "no-cache")
                .WithHeader("Connection", "keep-alive")
                .WithSseBody(async (_, queue) =>
                {
                    for (var i = 1; i <= 3; i++)
                    {
                        queue.Write($"x {i};\r\n");
                        await Task.Delay(100);
                    }

                    queue.Close();
                })
            );

        server
            .WhenRequest(r => r
                .UsingGet()
            )
            .ThenRespondWith(r => r
                .WithBody("normal")
            );

        using var client = new HttpClient();

        // Act 1
        var normal = await client.GetAsync(server.Url, _ct);
        (await normal.Content.ReadAsStringAsync(_ct)).Should().Be("normal");

        // Act 2
        using var response = await client.GetStreamAsync($"{server.Url}/sse", _ct);
        using var reader = new StreamReader(response);

        var data = string.Empty;
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            data += line;
        }

        // Assert 2
        data.Should().Be("x 1;x 2;x 3;");
    }
}