using Bit.BlazorUI;
using JSMR.UI.Blazor;
using JSMR.UI.Blazor.Services;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MudBlazor.Services;
using Radzen;

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

//string apiBase = builder.Configuration["ApiBaseUrl"] ?? builder.HostEnvironment.BaseAddress;
//Console.WriteLine(apiBase);

// If ApiBaseUrl is set (VS dev), use it. Otherwise (docker/prod), use same-origin.
var apiBase = builder.Configuration["ApiBaseUrl"];

if (string.IsNullOrWhiteSpace(apiBase))
    apiBase = builder.HostEnvironment.BaseAddress;

// Old way
//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(apiBase) });

// Register the handler
builder.Services.AddTransient<IncludeRequestCredentialsMessageHandler>();

//builder.Services.AddSingleton<TokenStore>();
//builder.Services.AddTransient<JwtAuthorizationMessageHandler>();

// Register a named client that uses the handler
builder.Services.AddHttpClient("Api", client =>
{
    client.BaseAddress = new Uri(apiBase);
})
.AddHttpMessageHandler<IncludeRequestCredentialsMessageHandler>();
//.AddHttpMessageHandler<JwtAuthorizationMessageHandler>();

// Keep your existing pattern (inject HttpClient) by mapping it to the named client
builder.Services.AddScoped(sp =>
    sp.GetRequiredService<IHttpClientFactory>().CreateClient("Api")
);

builder.Services.AddScoped<AuthenticationClient>();
builder.Services.AddScoped<SessionState>();

builder.Services.AddMudServices();
builder.Services.AddRadzenComponents();
builder.Services.AddBitBlazorUIServices();
builder.Services.AddAntDesign();

builder.Services.AddScoped<VoiceWorksClient>();
builder.Services.AddScoped<ILookupDataService, LookupDataService>();

await builder.Build().RunAsync();