using Freman.Sample.Web.Client.Pages;
using Freman.Sample.Web.Components;
using Freman.Sample.Web.Data;
using Freman.Sample.Web.Endpoints;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents();

builder.Services.AddDbContext<AppDbContext>(options =>
{
    var cs = builder.Configuration.GetConnectionString("AppDb");
    options.UseNpgsql(cs);
});


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    // Auto-apply migrations for dev/demo convenience.
    await DbMigrationHelper.ApplyMigrationsAsync(app.Services);
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}


// Only rewrite status codes for non-API routes (UI). APIs should return their real status codes.
app.UseWhen(
    context => !context.Request.Path.StartsWithSegments("/api"),
    uiApp => uiApp.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true));

app.UseHttpsRedirection();

app.UseAntiforgery();

// Map feature endpoints
app.MapNotesEndpoints();

app.MapStaticAssets();
app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode()
    .AddInteractiveWebAssemblyRenderMode()
    .AddAdditionalAssemblies(typeof(Freman.Sample.Web.Client._Imports).Assembly);

app.Run();

public partial class Program;