using ReportingServices.Shared.Repositories;
using Serilog;

LoggerConfiguration loggerConfiguration = new();

var builder = WebApplication.CreateBuilder(args);
Environment.SetEnvironmentVariable("workingDirectory", builder.Configuration.GetValue<string>("LoggingDirectory"));
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, builder.Configuration);
_ = SerilogHostBuilderExtensions.UseSerilog(builder.Host);
Log.Logger = loggerConfiguration.CreateLogger();
Serilog.ILogger log = Log.ForContext<Program>();


// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddScoped<IScrapeDatabaseRepository, ScrapeDatabaseRepository>();
builder.Services.AddScoped<IFabTimeReportingRepository, FabTimeReportingRepository>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
_ = app.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush);
log.Information("Starting Web Application");
try
{
    app.Run();
}
catch (Exception ex)
{
    log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
    Log.CloseAndFlush();
}
