using AppLibs.Libs;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Server.Kestrel.Core;

await WSNavigation.LoadJson();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers(options =>
{
    options.Filters.Add<AsyncGateAttribute>();
});
#if (!DEBUG)
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders =
        ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    // Nếu muốn an toàn hơn, chỉ trust proxy cụ thể:
    // options.KnownProxies.Add(IPAddress.Parse("127.0.0.1"));
});
builder.WebHost.UseKestrel(o =>
{
    o.ListenAnyIP(5111, lo =>
    {
        lo.Protocols = HttpProtocols.Http1;
    });
});
builder.Services.AddControllersWithViews();
#else
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
#endif

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.UseForwardedHeaders();
app.UseHttpsRedirection();
app.UseStaticFiles(); 
app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "mapCompany",
    pattern: "Company/{id?}",
    defaults: new { controller = "Company", action = "Index" })
    .WithStaticAssets();
app.MapControllerRoute(
    name: "mapServices",
    pattern: "Services/{id?}",
    defaults: new { controller = "Services", action = "Index" })
    .WithStaticAssets();

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


app.Run();
