using Newtonsoft.Json;
using PlanTempus.Core.CommandQueries;
using Shouldly;

namespace PlanTempus.X.TDD.CommandQueryHandlerTests;

[TestClass]
public class ProblemDetailsTests
{
    [TestMethod]
    public void TestFormatOfProblemDetails()
    {
        // Arrange
        var problemDetails = new ProblemDetails
        {
            Type = "https://example.com/errors/invalid-input",
            Title = "Invalid Input",
            Status = 400,
            Detail = "The request body is invalid.",
            Instance = "/api/users"
        };

        problemDetails.AddExtension("invalidFields", new[]
        {
            new { Field = "name", Message = "The 'name' field is required." },
            new { Field = "email", Message = "The 'email' field must be a valid email address." }
        });

        var json = JsonConvert.SerializeObject(problemDetails, Formatting.Indented);

        var expectedJson = """
                           {
                             "Type": "https://example.com/errors/invalid-input",
                             "Title": "Invalid Input",
                             "Status": 400,
                             "Detail": "The request body is invalid.",
                             "Instance": "/api/users",
                             "invalidFields": [
                               {
                                 "Field": "name",
                                 "Message": "The 'name' field is required."
                               },
                               {
                                 "Field": "email",
                                 "Message": "The 'email' field must be a valid email address."
                               }
                             ]
                           }
                           """;

        JsonConvert.DeserializeObject(json).ShouldBeSameAs(JsonConvert.DeserializeObject(expectedJson));
    }
}