using Microsoft.Extensions.Logging;
using SilverOS.Welcome.Core.Apply;
using SilverOS.Welcome.Core.Apps;
using SilverOS.Welcome.Core.Flavours;
using SilverOS.Welcome.Core.Preconfig;
using SilverOS.Welcome.App.Components;

namespace SilverOS.Welcome.App;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        // Redirect WebView2 user-data folder off Program Files (not writable at runtime)
        // to a per-user writable path so the embedded browser can always create its data dir.
        var wv2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
            "SilverOS", "Welcome", "WebView2");
        Directory.CreateDirectory(wv2);
        Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", wv2);

        // Cold-start + air-gap: prefer a fixed-version WebView2 runtime baked next to
        // the app (build.ps1 stages it under .\webview2). This removes two first-boot
        // costs at once: (1) the Evergreen-runtime registry probe, and (2) the risk
        // that IoT Enterprise LTSC ships WITHOUT WebView2 entirely — in which case the
        // wizard would block/blank waiting on an on-demand install. If the baked folder
        // is absent (VM/dev test), fall through to whatever Evergreen runtime exists.
        var fixedRuntime = Path.Combine(AppContext.BaseDirectory, "webview2");
        if (File.Exists(Path.Combine(fixedRuntime, "msedgewebview2.exe")))
        {
            Environment.SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", fixedRuntime);
            Diag.Log("WebView2: using baked fixed-version runtime at " + fixedRuntime);
        }
        else
        {
            Diag.Log("WebView2: no baked runtime found; relying on installed Evergreen");
        }

        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });

        builder.Services.AddMauiBlazorWebView();

#if DEBUG
        builder.Services.AddBlazorWebViewDeveloperTools();
        builder.Logging.AddDebug();
#endif

        builder.Services.AddSingleton<IProcessRunner, ProcessRunner>();
        builder.Services.AddSingleton<IFlavourLoader, FlavourLoader>();
        builder.Services.AddSingleton<IAppCatalog, AppCatalog>();
        builder.Services.AddSingleton<IBitLockerService, BitLockerService>();
        var appsDir = Path.Combine(AppContext.BaseDirectory, "apps");
        builder.Services.AddSingleton<IAppInstaller>(sp => new AppInstaller(sp.GetRequiredService<IProcessRunner>(), appsDir));
        builder.Services.AddSingleton<IApplyService>(sp => new ApplyService(
            sp.GetRequiredService<IBitLockerService>(),
            sp.GetRequiredService<IAppInstaller>()));
        builder.Services.AddSingleton<IPreconfigStore>(_ => new PreconfigStore(@"C:\ProgramData\SilverMetal"));
        builder.Services.AddScoped<WizardState>();

        return builder.Build();
    }
}
