@inject NavigationManager Nav
@implements IDisposable

@if (IsMobile)
{
    <nav class="p-4 space-y-4 text-sm">
        <Heading Level="4" Class="mb-2 text-sm">Blocks</Heading>

        <div class="space-y-1">
            <div class="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground px-2 pb-1">2.0 composed</div>
            <ul class="space-y-1">
                @foreach (var item in _composedBlocks)
                {
                    <li><NavLink href="@item.Href" class="@LinkClass(item.Href)">@item.Label</NavLink></li>
                }
            </ul>
        </div>

        <div class="space-y-1">
            <div class="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground px-2 pb-1">Full pages</div>
            <ul class="space-y-1">
                @foreach (var item in _patternBlocks)
                {
                    <li><NavLink href="@item.Href" class="@LinkClass(item.Href)">@item.Label</NavLink></li>
                }
            </ul>
        </div>
    </nav>
}
else
{
    <aside class="hidden lg:block w-64 shrink-0 m-3 rounded-[var(--radius-xl)] border border-border/60 bg-card shadow-lg overflow-y-auto sticky top-[84px]" style="max-height: calc(100vh - 100px)">
        <nav class="p-4 space-y-4 text-sm">
            <Heading Level="4" Class="mb-2 text-sm">Blocks</Heading>

            <div class="space-y-1">
                <div class="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground px-2 pb-1">2.0 composed</div>
                <ul class="space-y-1">
                    @foreach (var item in _composedBlocks)
                    {
                        <li><NavLink href="@item.Href" class="@LinkClass(item.Href)">@item.Label</NavLink></li>
                    }
                </ul>
            </div>

            <div class="space-y-1">
                <div class="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground px-2 pb-1">Full pages</div>
                <ul class="space-y-1">
                    @foreach (var item in _patternBlocks)
                    {
                        <li><NavLink href="@item.Href" class="@LinkClass(item.Href)">@item.Label</NavLink></li>
                    }
                </ul>
            </div>
        </nav>
    </aside>
}

@code {
    [Parameter] public bool IsMobile { get; set; }

    private record NavItem(string Label, string Href);

    private static readonly NavItem[] _composedBlocks =
    [
        new("Sign In",         "blocks/sign-in"),
        new("Sign Up",         "blocks/sign-up"),
        new("Reset Password",  "blocks/reset-password"),
        new("OTP Verify",      "blocks/otp-verify"),
        new("Pricing Table",   "blocks/pricing"),
        new("Hero Section",    "blocks/hero"),
        new("Dashboard",       "blocks/dashboard-block"),
        new("Settings Page",   "blocks/settings-page"),
    ];

    private static readonly NavItem[] _patternBlocks =
    [
        new("Analytics",       "blocks/analytics"),
        new("Authentication",  "blocks/authentication"),
        new("Calendar",        "blocks/calendar"),
        new("Chat",            "blocks/chat"),
        new("Dashboard (full)","blocks/dashboard"),
        new("E-Commerce",      "blocks/ecommerce"),
        new("File Manager",    "blocks/file-manager"),
        new("Filters",         "blocks/filters"),
        new("Form Wizard",     "blocks/form-wizard"),
        new("Kanban Board",    "blocks/kanban"),
        new("Mail",            "blocks/mail"),
        new("Music Player",    "blocks/music"),
        new("Notifications",   "blocks/notifications"),
        new("Settings",        "blocks/settings"),
        new("Social Feed",     "blocks/social-feed"),
        new("Task Tracker",    "blocks/task-tracker"),
    ];

    private string CurrentPath => Nav.ToBaseRelativePath(Nav.Uri).TrimEnd('/').ToLowerInvariant();

    protected override void OnInitialized()
    {
        Nav.LocationChanged += OnLocationChanged;
    }

    private void OnLocationChanged(object? sender, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e)
    {
        InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        Nav.LocationChanged -= OnLocationChanged;
    }

    private string LinkClass(string href)
    {
        var isActive = CurrentPath.Equals(href, StringComparison.OrdinalIgnoreCase);
        return isActive
            ? "block rounded-md px-2 py-1.5 bg-accent text-foreground font-medium transition-colors no-underline"
            : "block rounded-md px-2 py-1.5 text-muted-foreground hover:text-foreground hover:bg-accent transition-colors no-underline";
    }
}
