using clivelancaster.Data;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// Add PostgreSQL support
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));

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.UseCors();
app.UseStaticFiles(new StaticFileOptions
{
    ServeUnknownFileTypes = true, // Allows serving unknown file types
    DefaultContentType = "application/octet-stream", // Default content type
    OnPrepareResponse = ctx =>
    {
        var path = ctx.File.PhysicalPath.ToLowerInvariant();
        if (path.EndsWith(".woff"))
        {
            ctx.Context.Response.ContentType = "font/woff";
        }
        else if (path.EndsWith(".woff2"))
        {
            ctx.Context.Response.ContentType = "font/woff2";
        }
        else if (path.EndsWith(".ico"))
        {
            ctx.Context.Response.ContentType = "image/x-icon";
        }
        else if (path.EndsWith(".jpg") || path.EndsWith(".jpeg"))
        {
            ctx.Context.Response.ContentType = "image/jpeg";
        }
    }
});

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
