using Kalaha.Frontend;
using Kalaha.Frontend.Security;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.JSInterop;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

// 1. Unseren Custom Handler registrieren
builder.Services.AddTransient(sp =>
{
    var jsRuntime = sp.GetRequiredService<IJSRuntime>();
    var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
    var baseAddress = builder.HostEnvironment.BaseAddress;
    return new JwtAuthorizationHandler(jsRuntime, httpClientFactory, baseAddress);
});

// 2. Den HttpClient konfigurieren und den Handler einbinden
builder.Services.AddHttpClient("KalahaApi", client =>
{
    // WICHTIG: Trage hier die exakte URL deiner lokalen Backend-API ein!
    //local test with docker db:
    //client.BaseAddress = new Uri("http://localhost:5246");
    client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
})
.AddHttpMessageHandler<JwtAuthorizationHandler>();

// 3. Den konfigurierten Client als Standard für die App verfügbar machen
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("KalahaApi"));

await builder.Build().RunAsync();