using System.Text;

namespace SatelliteProvider.IntegrationTests;

// AZ-812: wire-format rename for POST /api/satellite/request.
// `RequestRegionRequest` now uses `lat`/`lon` (OSM convention) on the wire,
// replacing the previous verbose `latitude`/`longitude`. The strict-parsing
// infrastructure landed by AZ-795 (UnmappedMemberHandling.Disallow +
// GlobalExceptionHandler) means the old wire format must now be rejected
// explicitly, not silently coerced. AC-4 from the AZ-812 task spec.
public static class RegionFieldRenameTests
{
    private const string RegionPath = "/api/satellite/request";

    public static async Task RunAll(HttpClient httpClient)
    {
        RouteTestHelpers.PrintTestHeader("Test: Region endpoint OSM field-name rename (AZ-812)");

        await NewLatLonFormat_Returns200(httpClient);
        await OldLatitudeLongitudeFormat_Returns400(httpClient);

        Console.WriteLine("✓ Region field-rename tests: PASSED");
    }

    private static async Task NewLatLonFormat_Returns200(HttpClient httpClient)
    {
        Console.WriteLine();
        Console.WriteLine("AZ-812 AC-4 (positive): new {lat,lon} wire format → HTTP 200");

        // Arrange
        var regionId = Guid.NewGuid();
        var body = $"{{\"id\":\"{regionId}\",\"lat\":47.461747,\"lon\":37.647063,\"sizeMeters\":200,\"zoomLevel\":18,\"stitchTiles\":false}}";

        // Act
        var response = await PostJsonAsync(httpClient, body);
        var status = (int)response.StatusCode;
        var responseBody = await response.Content.ReadAsStringAsync();

        // Assert
        if (status != 200)
        {
            throw new Exception($"AZ-812 AC-4 positive: expected HTTP 200 for {{lat,lon}} body, got {status}. Body: {responseBody}");
        }

        Console.WriteLine("  ✓ {lat,lon} body accepted with HTTP 200");
    }

    private static async Task OldLatitudeLongitudeFormat_Returns400(HttpClient httpClient)
    {
        Console.WriteLine();
        Console.WriteLine("AZ-812 AC-4 (negative): legacy {latitude,longitude} wire format → HTTP 400 (UnmappedMemberHandling.Disallow)");

        // Arrange — exact pre-AZ-812 wire format; must now fail explicitly instead
        // of silently mapping to the renamed Lat/Lon properties.
        var regionId = Guid.NewGuid();
        var body = $"{{\"id\":\"{regionId}\",\"latitude\":47.461747,\"longitude\":37.647063,\"sizeMeters\":200,\"zoomLevel\":18,\"stitchTiles\":false}}";

        // Act
        var response = await PostJsonAsync(httpClient, body);
        var problem = await ProblemDetailsAssertions.ReadProblemDetailsAsync(response, "AZ-812 legacy field names");

        // Assert
        ProblemDetailsAssertions.AssertValidationProblem(problem, expectedStatus: 400, label: "AZ-812 legacy field names");
        ProblemDetailsAssertions.AssertErrorsContainsMention(problem, expectedMention: "latitude", label: "AZ-812 legacy field names");

        Console.WriteLine("  ✓ Legacy {latitude,longitude} body rejected with HTTP 400; errors map names the unknown field");
    }

    private static Task<HttpResponseMessage> PostJsonAsync(HttpClient httpClient, string body)
    {
        var content = new StringContent(body, Encoding.UTF8, "application/json");
        return httpClient.PostAsync(RegionPath, content);
    }
}
