using Microsoft.EntityFrameworkCore;

using Monitoring.Infrastructure.Data;
using Monitoring.Infrastructure.Services;

using Serilog;

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

Log.Information("Start to migrations");

try
{
	var builder = Host.CreateDefaultBuilder(args);

	var host = builder
		.ConfigureAppConfiguration(
			(context, builder) =>
			{
				builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
			}
		)
		.ConfigureServices((context, services )=>
		{
			_ = services.AddNpgsql<DataContext>(
				context.Configuration.GetConnectionString("DefaultConnection")
					?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.")
			);

			services.AddInfrastructureServices();
		})
		.Build();

	var dataContext = host.Services.GetRequiredService<DataContext>();

	// await dataContext.Database.MigrateAsync();

	await host.RunAsync();

	Log.Information("Migration complete");
}
catch (Exception ex)
{
	Log.Fatal(ex, "Application start-up failed");
}
finally
{
	Log.CloseAndFlush();
}
