using Microsoft.AspNetCore.Components.Web;
using SelectPdf;
using SelectPdf.Samples;
using SelectPdf.Samples.Components;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<HtmlRenderer>();
builder.Services.AddScoped<IComponentHtmlRenderer, ComponentHtmlRenderer>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    app.UseHsts();
}

ServerInfo.ServerPath = app.Environment.ContentRootPath;
ServerInfo.WebRootPath = app.Environment.WebRootPath ?? string.Empty;

// Apply the DenyLocalFileAccess flag from appsettings.json globally.
// When true, SelectPdf rejects any attempt to convert local files / file:// URLs.
GlobalProperties.ForceDenyLocalFileAccess =
    app.Configuration.GetValue<bool>("DenyLocalFileAccess", false);

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

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

// HTTP-inspection helper endpoints. These are reached by the SelectPdf
// converter as external HTTP requests (HttpPostRequest / HttpHeaders /
// HttpCookies samples) so they can't go through Blazor's antiforgery-
// protected SSR pipeline. Implemented as Minimal APIs that render the
// same site shell as the rest of the sample.
app.MapMethods("/ViewHttpPostData", new[] { "GET", "POST" }, ViewHelpers.ViewHttpPostData)
    .DisableAntiforgery();

app.Run();
