// Copyright (c) Microsoft. All rights reserved.

using System.Reflection;
using Microsoft.Extensions.Configuration;

namespace Shared.Workflows;

internal static class Application
{
    /// <summary>
    /// Configuration key used to identify the Foundry project endpoint.
    /// </summary>
    public static class Settings
    {
        public const string FoundryEndpoint = "FOUNDRY_PROJECT_ENDPOINT";
        public const string FoundryModel = "FOUNDRY_MODEL";
        public const string FoundryGroundingTool = "FOUNDRY_GROUNDING_TOOL";
    }

    public static string GetInput(string[] args)
    {
        string? input = args.FirstOrDefault();

        try
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;

            Console.Write("\nINPUT: ");

            Console.ForegroundColor = ConsoleColor.White;

            if (!string.IsNullOrWhiteSpace(input))
            {
                Console.WriteLine(input);
                return input;
            }
            while (string.IsNullOrWhiteSpace(input))
            {
                input = Console.ReadLine();

                // Exit gracefully when stdin is closed (e.g. automated test runner).
                if (input is null)
                {
                    return string.Empty;
                }
            }

            return input.Trim();
        }
        finally
        {
            Console.ResetColor();
        }
    }

    public static string? GetRepoFolder()
    {
        DirectoryInfo? current = new(Directory.GetCurrentDirectory());

        while (current is not null)
        {
            if (Directory.Exists(Path.Combine(current.FullName, ".git")))
            {
                return current.FullName;
            }

            current = current.Parent;
        }

        return null;
    }

    public static string GetValue(this IConfiguration configuration, string settingName) =>
        configuration[settingName] ??
        throw new InvalidOperationException($"Undefined configuration setting: {settingName}");

    /// <summary>
    /// Initialize configuration and environment
    /// </summary>
    public static IConfigurationRoot InitializeConfig() =>
        new ConfigurationBuilder()
            .AddUserSecrets(Assembly.GetExecutingAssembly())
            .AddEnvironmentVariables()
            .Build();
}
