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.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IFabTimeReportingRepository, FabTimeReportingRepository>();
builder.Services.AddScoped<IScrapeDatabaseRepository, ScrapeDatabaseRepository>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();
_ = app.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush);
log.Information("Starting Web APIs");

try
{
    app.Run();
}
catch (Exception ex)
{
    log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
    Log.CloseAndFlush();
}
