using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using ms_mappingservice.Mapping;
using webApiGateway.Objects.Objects;

namespace ms_mappingservice.Controllers;

/// <summary>
/// Controller for generating web requests related to mapping the query to the log source.
/// </summary>
[ApiController]
[Route("/mapping")]
public class WebController : ControllerBase
{
    /// <summary>
    /// Represents the logger instance for this class.
    /// </summary>
    private static ILogger Logger = Logging.LoggerFactory.CreateLogger<WebController>();
    
    /// <summary>
    /// Endpoint that receives a log source and returns a query string for it, which is later used for mapping log sources in another microservice.
    /// </summary>
    /// <param name="logSource">The log source object to map the query.</param>
    /// <returns>The query string corresponding to the log source.</returns>
    [HttpPost]
    public async Task<string> GetLogSourceQuery([FromBody] LogSource logSource)
    {
        Logger.LogDebug("Input log source: {0}", JsonSerializer.Serialize(logSource)); //Log input
        string query = await QueryMapping.GetQuery(logSource); //Getting the query
        Logger.LogInformation("Query returned: " + query); //Log output
        return query;
    }
}
