@model SharedKernel.Constants.Menu
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
@{
    // 현재 요청 경로 가져오기
    var currentPath = HttpContextAccessor.HttpContext?.Request?.Path.ToString()?.TrimEnd('/');

    // 고정된 메뉴 경로
    var menuPath = (Model.Path?.TrimEnd('/') ?? string.Empty).ToLower();

    bool isActive = false;
    bool isParentActive = false;

    // 특별 처리: navtabs가 있는 메뉴 (하위 경로가 메뉴 Path와 다른 경우)
    var multiPathMenus = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
    {
        { "게시판 관리", new[] { "/forum/board/meta", "/forum/board/prefix", "/forum/board/manager", "/forum/board/list" } },
        { "FAQ 관리", new[] { "/faq/list", "/faq/category" } },
        { "배너 관리", new[] { "/banner/list", "/banner/position" } },
        { "코인 목록", new[] { "/crypto/list", "/crypto/board" } }
    };

    if (multiPathMenus.TryGetValue(Model.Name, out var activePrefixes))
    {
        isActive = currentPath != null && activePrefixes.Any(prefix =>
            currentPath.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
        );
    }
    else if (Model.Children != null && Model.Children.Any())
    {
        // 자식 메뉴 중 하나가 활성화되었는지 확인
        isParentActive = Model.Children.Any(child =>
            !string.IsNullOrEmpty(currentPath) &&
            !string.IsNullOrEmpty(child.Path) &&
            (currentPath.Equals(child.Path, StringComparison.OrdinalIgnoreCase) || currentPath.StartsWith(child.Path + "/", StringComparison.OrdinalIgnoreCase))
        );

        // 부모 메뉴 자체는 active 주지 않음
        isActive = false;
    }
    else
    {
        // 일반 메뉴 active 여부 판단
        isActive = !string.IsNullOrEmpty(currentPath) && !string.IsNullOrEmpty(menuPath) && (currentPath.Equals(menuPath, StringComparison.OrdinalIgnoreCase) || currentPath.StartsWith(menuPath + "/", StringComparison.OrdinalIgnoreCase));
    }
}
<li class="nav-item">
    <a href="@Model.Path" class="nav-link @(isActive ? "active" : "") @Html.Raw(Model.Children == null || !Model.Children.Any() ? "collapsed" : "")"
        @Html.Raw(Model.Children != null && Model.Children.Any() ? "data-bs-toggle=\"collapse\"" : "")
        @Html.Raw(Model.Children != null && Model.Children.Any() ? $"data-bs-target=\"#menu-{Model.Id}\"" : "")
    >
        @if (!string.IsNullOrEmpty(Model.Icon))
        {
            @Html.Raw(Model.Icon);
        }

        @Model.Name
    </a>

    @if (Model.Children != null && Model.Children.Any())
    {
        <ul id="menu-@Model.Id" class="nav flex-column flex-nowrap ps-3 collapse">
            @foreach (var child in Model.Children)
            {
                @* @Html.Partial("_MenuItem", child); *@
                <partial name="_MenuItem" model="child" />
            }
        </ul>
    }
</li>