using DataSharing.Host;
using Serilog;

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

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

    var builder = WebApplication.CreateBuilder(args);

    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))
        ;

    builder
        .ConfigureServices()
        .ConfigurePipelines()
        .Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "Unhandled exception");
}
finally
{
    Log.Information("Shut down complete");
    Log.CloseAndFlush();
}