using Hotline.Api;
using Serilog;
using XF.Domain.Authentications;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateBootstrapLogger();

try
{
    Log.Information("Hotline service is Starting up");

    var builder = WebApplication.CreateBuilder(args);

    builder.WebHost.ConfigureKestrel(serverOptions =>
    {
        serverOptions.Limits.MaxConcurrentConnections = 5000;
        serverOptions.Limits.MaxConcurrentUpgradedConnections = 5000;
    });

    builder.Host
        .ConfigureAppConfiguration((hostBuilderContext, configBuilder) =>
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "config");
            configBuilder.SetBasePath(path)
#if DEBUG
                .AddJsonFile("appsettings.shared.Development.json", true, true)
#else
                .AddJsonFile("appsettings.shared.json", true, true)
#endif
                .AddJsonFile("appsettings.json", false, true)
                .AddJsonFile($"appsettings.{hostBuilderContext.HostingEnvironment.EnvironmentName}.json", true, true)
                .AddEnvironmentVariables()
                .AddCommandLine(args)
                ;
        })
        .UseSerilog((ctx, lc) => lc
        //.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
        .Enrich.FromLogContext()
        .ReadFrom.Configuration(ctx.Configuration), true)
        ;
    //builder.Services.AddControllers().AddNewtonsoftJson();
    builder
        .ConfigureServices()
        .ConfigurePipelines()
        .Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "Unhandled exception");
}
finally
{
    Log.Information("Shut down complete");
    Log.CloseAndFlush();
}
