using Microsoft.EntityFrameworkCore;
using WebApp.Data;
using WebApp.Endpoints;
using WebApp.Entities;

// Create WebAppliaction Builder
var builder = WebApplication.CreateBuilder(args);

// Configure Database Conecction
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseNpgsql(connectionString));

// Add Developer Exception Filter
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

// Configure Identity
//builder.Services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = true)
//    .AddEntityFrameworkStores<ApplicationDbContext>();

// API Services For Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "hermes", Version = "v1" });
    });

builder.Services.AddScoped<GeneralUseHelpers>();

// Build Application
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "hermes v1"));
}
else
{
    app.UseHsts();
}

// Middleware Configuration
app.UseHttpsRedirection(); // Redirects all HTTP requests to HTTPS
app.UseDefaultFiles(); // Serves default files (index.html) if no specific file is requested
app.UseStaticFiles(); // Serves static files(CSS, JS, Img) from the wwwroot folder.
app.UseRouting(); // Enables routing to match incoming request to endpoints
//app.UseAuthorization(); 

// Map Minimal API Endpoints
app.MapEventsEndpoints();
app.MapOrganizationsEndpoints();
app.MapAuthEndpoints();
app.MapEventsRegistrationEndpoints();

app.Run();
