///TODO:
/// - Refine the home page/about page editor to use the API instead of embeding images
/// - Add auth back to the admin pages

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MudBlazor.Services;
using MyAppName.WebApp.Components.Account;
using OpenArchival.Blazor;
using OpenArchival.Blazor.AdminPages;
using OpenArchival.Blazor.AdminPages.Shared;
using OpenArchival.Blazor.ArchiveSearch;
using OpenArchival.Blazor.Components;
using OpenArchival.Blazor.Config;
using OpenArchival.Blazor.CustomComponents;
using OpenArchival.DataAccess;

var builder = WebApplication.CreateBuilder(args);

// --- UI Services ---
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddMudServices();
builder.Services.AddMudExtensions();
builder.Services.AddControllers();

// --- Database & Identity Configuration ---
// Get the single connection string for your PostgreSQL database.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");

// Use AddDbContextFactory for Blazor Server. This is safer for managing DbContext lifecycles.
// This single ApplicationDbContext will handle both Identity and your application data.
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
    options.UseNpgsql(connectionString), ServiceLifetime.Scoped);

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

// Configure Identity to use ApplicationDbContext, which is pointed at PostgreSQL.
builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddSignInManager()
    .AddDefaultTokenProviders();

builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = IdentityConstants.ApplicationScheme;
    options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();

// --- File Upload Configuration ---
builder.Services.AddOptions<FileUploadOptions>().Bind(builder.Configuration.GetSection(FileUploadOptions.Key));
builder.Services.AddOptions<ApplicationOptions>().Bind(builder.Configuration.GetSection(ApplicationOptions.Key));

var uploadSettings = builder.Configuration.GetSection(FileUploadOptions.Key).Get<FileUploadOptions>() ?? throw new ArgumentNullException("FileUploadOptions");
builder.Services.AddServerSideBlazor().AddHubOptions(options =>
{
    options.MaximumReceiveMessageSize = uploadSettings.MaxUploadSizeBytes;
    options.MaximumReceiveMessageSize = 1024 * 1024 * 10; // 10MB
    options.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
    options.HandshakeTimeout = TimeSpan.FromSeconds(30);
});

// --- Custom Application Services ---
builder.Services.AddScoped<IArchiveCategoryProvider, ArchiveCategoryProvider>();
builder.Services.AddScoped<IFilePathListingProvider, FilePathListingProvider>();
builder.Services.AddScoped<IArtifactStorageLocationProvider, ArtifactStorageLocationProvider>();
builder.Services.AddScoped<IArtifactDefectProvider, ArtifactDefectProvider>();
builder.Services.AddScoped<IArtifactTypeProvider, ArtifactTypeProvider>();
builder.Services.AddScoped<IArchiveEntryTagProvider, ArchiveEntryTagProvider>();
builder.Services.AddScoped<IListedNameProvider, ListedNameProvider>();
builder.Services.AddScoped<ArtifactEntrySharedHelpers>();
builder.Services.AddScoped<IArtifactGroupingProvider, ArtifactGroupingProvider>();
builder.Services.AddScoped<ArtifactGroupingSearch>();
builder.Services.AddScoped<BlogPostSearch>();

builder.Services.AddLogging();

builder.Services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo("/app/keys"))
    .SetApplicationName("OpenArchival");

var app = builder.Build();

app.MapControllers();

using (var scope = app.Services.CreateScope())
{
    var serviceProvider = scope.ServiceProvider;
    var logger = scope.ServiceProvider.GetService<ILogger<Program>>();
    if (logger is null)
    {
        Console.WriteLine("Logger not found!");
        System.Environment.Exit(1);
    }
    // First migrate the database if there is a new migration available
    try
    {
        logger.LogInformation("Migrating database...");
        var factory = serviceProvider.GetRequiredService<IDbContextFactory<ApplicationDbContext>>();
        await using var context = await factory.CreateDbContextAsync(); 
        await context.Database.MigrateAsync();
    }    
    catch (Exception ex)
    {
        logger.LogError($"Failed to migrate database. Got error: {ex.Message}\n");
        System.Environment.Exit(1);
    }

    // Add the admin user
    try
    {
        await IdentityDataSeeder.SeedRolesAndAdminUserAsync(serviceProvider);
    }
    catch (Exception ex)
    {
        logger.LogError(ex, "An error occurred while seeding the database with admin credentials.");
    }
}

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddAdditionalAssemblies(typeof(OpenArchival.Blazor.AdminPages.ArchiveManagement).Assembly,
                            typeof(OpenArchival.Blazor.ArtifactGroupingDisplay.ArchiveGroupingDisplay).Assembly,
                            typeof(OpenArchival.Blazor.ArchiveSearch.SearchArchive).Assembly,
                            typeof(OpenArchival.Blazor.Blog.BlogEditor).Assembly)
    .AddInteractiveServerRenderMode();

app.MapAdditionalIdentityEndpoints();

app.MapPost("/Logout", async (Microsoft.AspNetCore.Identity.SignInManager<OpenArchival.DataAccess.ApplicationUser> signInManager) =>
{
    await signInManager.SignOutAsync();

    // Redirects the user back to the home page
    return Microsoft.AspNetCore.Http.Results.Redirect("/");
}).DisableAntiforgery();

app.Run();