using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using webApiGateway.HttpRequests;
using webApiGateway.Objects;
using webApiGateway.Objects.GUI;
using webApiGateway.Processing;

namespace webApiGateway.Controllers;

/// <summary>
/// This class represents a Web API controller for mapping data from the GUI to use cases, being a Web API gateway.
/// </summary>
[ApiController]
[Route("/wag")]
public class WebController : ControllerBase
{
    /// <summary>
    /// Represents the logger instance for this class.
    /// </summary>
    private static ILogger Logger = Logging.LoggerFactory.CreateLogger<WebController>();

    /// <summary>
    /// This method maps data from the GUI to use cases.
    /// </summary>
    /// <param name="input">The input data from the GUI.</param>
    /// <returns>The result of mapping the input data to use cases sent to GUI.</returns>
    [HttpPost]
    public async Task<IActionResult> MapLesToUseCases([FromBody] Data input)
    {
        // Log the input object.
        Logger.LogInformation("Input object: " + JsonConvert.SerializeObject(input)); 

        // Save the input data from GUI.
        GuiInput.Input = input;

        // Map the input data to use cases and get the output.
        Output output = new Output()
        {
            Results = await MainMapping.MapLesToUseCases()
        };

        // Log the output object.
        Logger.LogInformation("Output object: " + JsonConvert.SerializeObject(output)); 

        // Set the Access-Control-Allow-Headers header for CORS policy.
        Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type");

        // Return the output data to the GUI.
        return Content(JsonConvert.SerializeObject(output), "application/json"); 
    }
}
