using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using webApiGateway.Objects;
using webApiGateway.Objects.Objects;

namespace webApiGateway.HttpRequests;

/// <summary>
/// Class for communication with ms-lesService.
/// </summary>
public class LesService
{
    /// <summary>
    /// Represents the logger instance for this class.
    /// </summary>
    private static ILogger Logger = Logging.LoggerFactory.CreateLogger<LesService>();
    
    /// <summary>
    /// Sends the specified list of log sources to the endpoint URL.
    /// </summary>
    /// <param name="LogSources">The list of log sources to send.</param>
    /// <returns>A task representing the asynchronous operation.</returns>
    public static async Task SendLogSources(List<LogSource> LogSources)
    {
        // Serialize the log sources list to JSON.
        var settings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
        var json = JsonConvert.SerializeObject(LogSources, Formatting.Indented, settings);

        // Get the endpoint URL from the appsettings.json file.
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();
        var URL = configuration.GetValue<string>("EndpointUrls:LesService");

        // Send an HTTP POST request to the endpoint URL with the serialized JSON data.
        using var httpClient = new HttpClient();
        using var request = new HttpRequestMessage(new HttpMethod("POST"), URL)
        {
            Content = new StringContent(json, Encoding.UTF8, "application/json")
        };
        Logger.LogInformation("Log sources were sent to ms-lesService.");
    }
}