@using SilverOS.Welcome.Core.Apps
@inject WizardState State

<div class="step apps-step">
    <h1>Choose your apps</h1>
    <p class="step-subtitle">We'll install these during setup. The SilverLABS Stack (browser, VPN, keys) is already included.</p>

    @foreach (var grp in _groups)
    {
        <h3 class="apps-group">@GroupTitle(grp.Key)</h3>
        <div class="apps-grid">
            @foreach (var app in grp)
            {
                <label class="app-card @(State.SelectedApps.Contains(app.Id) ? "selected" : "")">
                    <input type="checkbox" checked="@State.SelectedApps.Contains(app.Id)"
                           @onchange="e => Toggle(app.Id, (bool)e.Value!)" />
                    <span class="app-name">@app.Name</span>
                    <span class="app-desc">@app.Description</span>
                </label>
            }
        </div>
    }
</div>

@code {
    [Parameter] public IReadOnlyList<AppCatalogEntry> Apps { get; set; } = Array.Empty<AppCatalogEntry>();

    private IEnumerable<IGrouping<string, AppCatalogEntry>> _groups =>
        Apps.GroupBy(a => a.Group).OrderByDescending(g => g.Key == "essentials");

    private static string GroupTitle(string g) => g switch
    {
        "essentials" => "Essentials",
        "developer" => "Developer tools",
        "journalist" => "Journalist tools",
        "daily-driver" => "Everyday apps",
        "privacy-max" => "Privacy tools",
        _ => g
    };

    void Toggle(string id, bool on)
    {
        if (on) State.SelectedApps.Add(id); else State.SelectedApps.Remove(id);
    }
}
