using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MudBlazor.Services;
using WebClientTpos.Client;
using WebClientTpos.Client.Localization;
using Microsoft.Extensions.Localization;
using System.Globalization;
using Microsoft.JSInterop;


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

// EN: Add HttpClient for API calls
// VI: Thêm HttpClient cho các cuộc gọi API
builder.Services.AddSingleton(sp => new HttpClient { BaseAddress = new Uri(new Uri(builder.HostEnvironment.BaseAddress).GetLeftPart(UriPartial.Authority)) });

// EN: Add POS data service for BFF API calls
// VI: Thêm POS data service cho BFF API calls
builder.Services.AddScoped<WebClientTpos.Client.Services.PosDataService>();
builder.Services.AddScoped<WebClientTpos.Client.Services.ReceiptPrintService>();

// EN: Add auth state service for role-based redirects
// VI: Thêm auth state service cho điều hướng theo vai trò
builder.Services.AddSingleton<WebClientTpos.Client.Services.AuthStateService>();

// EN: Add auth service for login/register API calls
// VI: Thêm auth service cho login/register API calls
builder.Services.AddScoped<WebClientTpos.Client.Services.AuthService>();

// EN: Add merchant API service for merchant/shop management
// VI: Thêm merchant API service cho quản lý merchant/shop
builder.Services.AddScoped<WebClientTpos.Client.Services.MerchantApiService>();

// EN: Add IAM API service for roles, audit, users
// VI: Thêm IAM API service cho roles, audit, users
builder.Services.AddScoped<WebClientTpos.Client.Services.IamApiService>();

// EN: Add Super Admin API service for platform management
// VI: Thêm Super Admin API service cho quản lý nền tảng
builder.Services.AddScoped<WebClientTpos.Client.Services.SuperAdminApiService>();

// EN: Add localStorage service — typed wrapper for JS localStorage (FRONT-W-07)
// VI: Thêm localStorage service — wrapper kiểu cho JS localStorage (FRONT-W-07)
builder.Services.AddScoped<WebClientTpos.Client.Services.LocalStorageService>();

// EN: Add theme state service — single MudThemeProvider in App.razor uses this (FRONT-W-06)
// VI: Thêm theme state service — MudThemeProvider duy nhất trong App.razor dùng service này (FRONT-W-06)
builder.Services.AddSingleton<WebClientTpos.Client.Services.ThemeStateService>();

// EN: Add MudBlazor services
// VI: Thêm các services của MudBlazor
builder.Services.AddMudServices();

// Localization
builder.Services.AddLocalization();
builder.Services.AddSingleton<LocalizationCache>();
builder.Services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>();

// Build the host
var host = builder.Build();

// Initialize Localization Cache
var cache = host.Services.GetRequiredService<LocalizationCache>();

// Default culture is Vietnamese
var culture = new CultureInfo("vi-VN");

// EN: Try reading saved culture preference via LocalStorageService (FRONT-W-07)
// VI: Thử đọc culture đã lưu qua LocalStorageService (FRONT-W-07)
try
{
    var localStorage = host.Services.GetRequiredService<WebClientTpos.Client.Services.LocalStorageService>();
    var savedCulture = await localStorage.GetAsync("aPOS_culture");
    if (!string.IsNullOrEmpty(savedCulture))
    {
        try { culture = new CultureInfo(savedCulture); } catch { }
    }
}
catch { /* JS not available yet during startup, use default */ }

CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

await cache.LoadAsync(culture);

await host.RunAsync();
