using MudBlazor;

namespace TaxBaik.Web.Client.Components.Admin.Shared;

public sealed record AdminNavigationItem(
    string Label,
    string Href,
    string Icon,
    string? Badge = null,
    bool IsExact = false,
    IReadOnlyList<string>? AllowedRoles = null);

public sealed record AdminNavigationSection(
    string Label,
    IReadOnlyList<AdminNavigationItem> Items);

public static class AdminNavigationCatalog
{
    public static IReadOnlyList<AdminNavigationSection> Sections { get; } =
    [
        new("개요", [
            new("대시보드", "/admin/dashboard", Icons.Material.Filled.Dashboard, IsExact: true),
        ]),
        new("CRM & 세무관리", [
            new("세무 프로필", "/admin/tax-profiles", Icons.Material.Filled.Badge, AllowedRoles: ["Admin", "Manager"]),
            new("신고 일정", "/admin/tax-filing-schedules", Icons.Material.Filled.EventNote, AllowedRoles: ["Admin", "Manager"]),
            new("계약 관리", "/admin/contracts", Icons.Material.Filled.Description, AllowedRoles: ["Admin", "Manager"]),
            new("상담 활동", "/admin/consulting-activities", Icons.Material.Filled.SupportAgent, AllowedRoles: ["Admin", "Manager"]),
            new("수익 추적", "/admin/revenue-trackings", Icons.Material.Filled.Payments, AllowedRoles: ["Admin"]),
        ]),
        new("고객 관리", [
            new("고객 카드", "/admin/clients", Icons.Material.Filled.Groups, AllowedRoles: ["Admin", "Manager", "Staff"]),
            new("세무신고", "/admin/tax-filings", Icons.Material.Filled.Assignment, AllowedRoles: ["Admin", "Manager", "Staff"]),
        ]),
        new("홈페이지", [
            new("공지사항", "/admin/announcements", Icons.Material.Filled.Campaign, AllowedRoles: ["Admin", "ContentManager"]),
            new("FAQ 관리", "/admin/faqs", Icons.Material.Filled.HelpOutline, AllowedRoles: ["Admin", "ContentManager"]),
            new("블로그 관리", "/admin/blog", Icons.Material.Filled.Article, AllowedRoles: ["Admin", "ContentManager"]),
            new("시즌 시뮬레이터", "/admin/season-simulator", Icons.Material.Filled.CalendarMonth, AllowedRoles: ["Admin", "ContentManager"]),
            new("문의 관리", "/admin/inquiries", Icons.Material.Filled.Inbox, AllowedRoles: ["Admin", "Manager", "Staff"]),
            new("클라이언트 로그", "/admin/client-logs", Icons.Material.Filled.BugReport, AllowedRoles: ["Admin"]),
            new("설정", "/admin/settings", Icons.Material.Filled.Settings, AllowedRoles: ["Admin"]),
            new("공통관리", "/admin/common-codes", Icons.Material.Filled.Apps, AllowedRoles: ["Admin"]),
        ]),
    ];

    public static bool IsActive(AdminNavigationItem item, string currentPath)
    {
        if (string.IsNullOrWhiteSpace(currentPath))
            return false;

        var normalizedPath = NormalizePath(currentPath);
        var normalizedHref = NormalizePath(item.Href);

        return item.IsExact
            ? string.Equals(normalizedPath, normalizedHref, StringComparison.OrdinalIgnoreCase)
            : normalizedPath.StartsWith(normalizedHref, StringComparison.OrdinalIgnoreCase);
    }

    public static string GetLabel(string currentPath)
    {
        var path = NormalizePath(currentPath);
        return path switch
        {
            "/" or "/admin" or "/admin/dashboard" or "/dashboard" => "대시보드",
            "/admin/inquiries" => "문의 관리",
            "/admin/clients" => "고객 관리",
            "/admin/blog" => "블로그 관리",
            "/admin/faqs" => "FAQ 관리",
            "/admin/announcements" => "공지사항",
            "/admin/tax-profiles" => "세무 프로필",
            "/admin/tax-filing-schedules" => "신고 일정",
            "/admin/contracts" => "계약 관리",
            "/admin/consulting-activities" => "상담 활동",
            "/admin/revenue-trackings" => "수익 추적",
            "/admin/common-codes" => "공통관리",
            "/admin/settings" => "설정",
            _ => "운영 화면"
        };
    }

    private static string NormalizePath(string path)
    {
        if (string.IsNullOrWhiteSpace(path))
            return "/";

        var normalized = path.Trim();
        if (!normalized.StartsWith('/'))
            normalized = "/" + normalized;

        return normalized.TrimEnd('/') is "" ? "/" : normalized.TrimEnd('/');
    }
}
