@using Microsoft.AspNetCore.Components.Authorization
@using TaxBaik.Web.Client.Components.Admin.Shared
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager Navigation
@implements IDisposable
<aside class="admin-drawer">
    <MudPaper Class="admin-drawer-brand" Elevation="0">
        <div class="admin-brand-mark">T</div>
        <div>
            <MudText Typo="Typo.subtitle2" Class="admin-drawer-title">TaxBaik</MudText>
            <MudText Typo="Typo.caption" Class="admin-drawer-subtitle">세무 운영 콘솔</MudText>
        </div>
    </MudPaper>

    <MudPaper Class="admin-drawer-context" Elevation="0" Outlined="true">
        <MudText Typo="Typo.overline" Class="admin-drawer-context-label">현재 화면</MudText>
        <MudText Typo="Typo.body2" Class="admin-drawer-context-value">@currentLabel</MudText>
    </MudPaper>

    <nav class="admin-nav" aria-label="관리자 메뉴">
        @foreach (var group in visibleSections)
        {
            <div class="admin-nav-group">@group.Label</div>
            @foreach (var item in group.Items)
            {
                <a class="admin-nav-link @(AdminNavigationCatalog.IsActive(item, currentPath) ? "active" : null)"
                   href="@item.Href"
                   aria-current="@(AdminNavigationCatalog.IsActive(item, currentPath) ? "page" : null)">
                    <MudIcon Icon="@item.Icon" Class="admin-nav-link-icon" />
                    <span class="admin-nav-link-label">@item.Label</span>
                    @if (!string.IsNullOrWhiteSpace(item.Badge))
                    {
                        <span class="admin-nav-badge">@item.Badge</span>
                    }
                </a>
            }
        }
    </nav>

    <MudPaper Class="admin-drawer-footer" Elevation="0">
        <a class="admin-footer-item" href="/" target="_blank" rel="noopener">공개 사이트</a>
        <a class="admin-footer-item" href="./logout">로그아웃</a>
    </MudPaper>
</aside>

@code {
    private IReadOnlyList<AdminNavigationSection> visibleSections = [];
    private string currentPath = "/";
    private string currentLabel = "대시보드";

    protected override void OnInitialized()
    {
        currentPath = ResolveCurrentPath();
        currentLabel = AdminNavigationCatalog.GetLabel(currentPath);
        Navigation.LocationChanged += OnLocationChanged;
    }

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;
        visibleSections = AdminNavigationCatalog.Sections
            .Select(section => section with
            {
                Items = section.Items.Where(item => item.AllowedRoles is null || item.AllowedRoles.Count == 0 || item.AllowedRoles.Any(user.IsInRole)).ToList()
            })
            .Where(section => section.Items.Count > 0)
            .ToList();
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs args)
    {
        currentPath = ResolveCurrentPath();
        currentLabel = AdminNavigationCatalog.GetLabel(currentPath);
        InvokeAsync(StateHasChanged);
    }

    private string ResolveCurrentPath()
    {
        var path = Navigation.ToBaseRelativePath(Navigation.Uri);
        return string.IsNullOrWhiteSpace(path) ? "/" : "/" + path;
    }

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