using ms_namingservice.Objects;
using webApiGateway.HttpRequests;
using webApiGateway.Objects.Objects;

namespace webApiGateway.Processing;

/// <summary>
/// The MainMapping class is responsible for mapping log sources to use cases, using external microservices.
/// </summary>
public class MainMapping
{
    /// <summary>
    /// Maps log sources to use cases by sending the log sources to the ms-lesService,
    /// communicating with the ms-mappingService and msUseCaseService, and obtaining
    /// the generated name for each use case from the ms-namingService.
    /// </summary>
    /// <returns>A list of Result objects, each containing a log source and its associated use cases.</returns>
    public static async Task<List<Result>> MapLesToUseCases()
    {
        // Sends the log sources to the ms-lesService to save them, if they are not saved.
        await LesService.SendLogSources(GuiInput.Input.LogSources);

        // Communicates with the ms-mappingService and msUseCaseService to obtain the list of use cases for each log source.
        var results = await MappingServiceCommunication();

        // Gets the generated name for each use case from the ms-namingService and assigns it to the use case.
        foreach (var result in results)
        {
            var les = result.LogSource;
            foreach (var uc in result.UseCases)
            {
                var namingInput = new Input
                {
                    Title = uc.Title,
                    Description = uc.Description,
                    Tags = uc.Tags,
                    LogSource = les
                };
                uc.GeneratedName = await NamingService.GetGeneratedName(namingInput);
            }
        }

        //Returning the value, sends it to the GUI via WebController.
        return results;
    }

    /// <summary>
    /// Communicates with the ms-mappingService and msUseCaseService to obtain the list of use cases for each log source.
    /// </summary>
    /// <returns>A list of <c>Result</c> objects, each containing a log source and its associated use cases.</returns>
    public static async Task<List<Result>> MappingServiceCommunication()
    {
        var results = new List<Result>();
        foreach (var les in GuiInput.Input.LogSources)
        {
            // Sends the log source to the ms-mappingService to obtain the query needed for the database.
            string query = await MappingService.SendLogSource(les);

            // Sends the query to the ms-useCaseService to obtain the list of use cases for the log source.
            List<UseCase> useCases = await UseCaseService.GetUseCases(query);

            // Combines the log source with its associated use cases.
            var result = new Result
            {
                LogSource = les,
                UseCases = useCases
            };
            
            //Adding the log source and its use cases to the result object and continuing the for loop, until all log sources are mapped
            results.Add(result);
        }

        //Returning the mapped use cases to the log source
        return results;
    }
}