@inject NavigationManager Navigation

<div class="nav-header">
    <a class="nav-brand" href="">
        <img src="logo.png" alt="GM-Relay" class="nav-brand-icon" />
        <span class="nav-brand-text">GM-Relay</span>
    </a>
    <button class="nav-toggle" @onclick="ToggleMenu" aria-label="Переключить меню">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <line x1="3" y1="6" x2="21" y2="6"/>
            <line x1="3" y1="12" x2="21" y2="12"/>
            <line x1="3" y1="18" x2="21" y2="18"/>
        </svg>
    </button>
</div>

<nav class="nav-body @(isOpen ? "open" : "")">
    <AuthorizeView>
        <Authorized>
            <div class="nav-section">
                <NavLink class="nav-item" href="" Match="NavLinkMatch.All" @onclick="CloseMenu">
                    <svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>
                        <polyline points="9 22 9 12 15 12 15 22"/>
                    </svg>
                    Главная страница
                </NavLink>
                <NavLink class="nav-item" href="templates" @onclick="CloseMenu">
                    <svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <rect x="3" y="4" width="18" height="16" rx="2"/>
                        <path d="M7 8h10"/>
                        <path d="M7 12h6"/>
                        <path d="M7 16h8"/>
                    </svg>
                    Шаблоны
                </NavLink>
                <NavLink class="nav-item" href="profile" @onclick="CloseMenu">
                    <svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
                        <circle cx="12" cy="7" r="4"/>
                    </svg>
                    Профиль
                </NavLink>

                <NavLink class="nav-item" href="profile/memberships" @onclick="CloseMenu">
                    <svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M3 21h18"/>
                        <path d="M5 21V7l8-4v18"/>
                        <path d="M19 21V11l-6-4"/>
                    </svg>
                    Мои клубы
                </NavLink>
            </div>

            <div class="nav-footer">
                <div class="nav-user">
                    <div class="nav-user-avatar">
                        @if (!string.IsNullOrWhiteSpace(context.User.GetAvatarUrl()))
                        {
                            <img src="@context.User.GetAvatarUrl()" alt="" />
                        }
                        else
                        {
                            @(context.User.Identity?.Name?.Substring(0, 1).ToUpper() ?? "?")
                        }
                    </div>
                    <div class="nav-user-info">
                        <span class="nav-user-name">@context.User.Identity?.Name</span>
                        <span class="nav-user-platform">@GetPlatformLabel(context.User)</span>
                    </div>
                </div>

                <form action="/auth/logout" method="post">
                    <AntiforgeryToken />
                    <button type="submit" class="nav-logout-btn">
                        <svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1-2 2h4"/>
                            <polyline points="16 17 21 12 16 7"/>
                            <line x1="21" y1="12" x2="9" y2="12"/>
                        </svg>
                        Выход
                    </button>
                </form>

                <div class="nav-version">v3.9.8</div>
            </div>
        </Authorized>
        <NotAuthorized>
            <div class="nav-section">
                <NavLink class="nav-item" href="login" @onclick="CloseMenu">
                    <svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/>
                        <polyline points="10 17 15 12 10 7"/>
                        <line x1="15" y1="12" x2="3" y2="12"/>
                    </svg>
                    Вход
                </NavLink>
            </div>
        </NotAuthorized>
    </AuthorizeView>
</nav>

@code {
    private bool isOpen;

    private void ToggleMenu() => isOpen = !isOpen;
    private void CloseMenu() => isOpen = false;

    private static string GetPlatformLabel(System.Security.Claims.ClaimsPrincipal user) =>
        user.TryGetDiscordId(out _) ? "Discord" : "Telegram";
}
