using Insight.Setup.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Runtime.Versioning;

namespace Insight.Setup.Windows
{
    [SupportedOSPlatform("windows")]
    public class Program
    {
        public static async Task Main(string[] args)
        {
            await Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(options =>
                {
                    options.Sources.Clear();
                    options.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    options.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true);

                    options.AddCommandLine(args, new Dictionary<string, string>()
                    {
                        { "-deploy", "deploy" },
                        { "--deploy", "deploy" }
                    });
                })
                .ConfigureLogging(options =>
                {
                    options.ClearProviders();
                    options.SetMinimumLevel(LogLevel.Trace);

                    options.AddSimpleConsole(options =>
                    {
                        options.IncludeScopes = true;
                        options.SingleLine = true;
                        options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff ";
                    });

                    options.AddFilter("Microsoft", LogLevel.Warning);
                })
                .ConfigureServices((host, services) =>
                {
                    // SERVICES
                    services.AddHostedService<SetupService>();

                    // GLOBALS
                    services.AddTransient(provider => new HttpClient(new HttpClientHandler
                    {
                        ClientCertificateOptions = ClientCertificateOption.Manual,
                        ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true
                    }));
                })
                .Build()
                .RunAsync();
        }
    }
}