@using Microsoft.AspNetCore.Components.Routing

<MudBreadcrumbs Items="_items" Class="pa-0 mb-2">
    <SeparatorTemplate>
        <MudIcon Icon="@Icons.Material.Filled.ChevronLeft" Size="Size.Small" />
    </SeparatorTemplate>
</MudBreadcrumbs>

@code {
    [Inject] private NavigationManager Nav { get; set; } = default!;

    private List<BreadcrumbItem> _items = new();

    protected override void OnParametersSet()
    {
        BuildBreadcrumbs();
    }

    private void BuildBreadcrumbs()
    {
        var uri = new Uri(Nav.Uri);
        var path = uri.AbsolutePath.TrimStart('/').TrimEnd('/');

        _items = new List<BreadcrumbItem>
        {
            new("خانه", "/", icon: Icons.Material.Filled.Home)
        };

        if (string.IsNullOrEmpty(path)) return;

        var segments = path.Split('/');
        var accumulated = "";

        foreach (var segment in segments)
        {
            accumulated += $"/{segment}";
            var title = GetPersianTitle(segment, accumulated);
            if (!string.IsNullOrEmpty(title))
            {
                _items.Add(new BreadcrumbItem(title, accumulated));
            }
        }

        // Mark last item as disabled (current page)
        if (_items.Count > 1)
        {
            var last = _items[^1];
            _items[^1] = new BreadcrumbItem(last.Text, href: null, disabled: true);
        }
    }

    private static string GetPersianTitle(string segment, string fullPath)
    {
        // Map URL segments to Persian titles
        return segment.ToLowerInvariant() switch
        {
            // Dashboard
            "dashboard" => "داشبورد",
            "system-overview" => "نمای کلی",

            // Users
            "users" => "کاربران",
            "user" => "کاربران",
            "user-address" => "آدرس‌ها",
            "user-role" => "نقش‌های کاربر",

            // Products
            "products" => "محصولات",
            "product-categories" => "دسته‌بندی محصول",
            "bulk-edit" => "ویرایش گروهی",

            // Categories
            "categories" => "دسته‌بندی‌ها",
            "category" => "دسته‌بندی‌ها",
            "category-products" => "محصولات دسته‌بندی",

            // Orders
            "orders" => "سفارش‌ها",
            "user-order" => "سفارش‌ها",

            // Packages
            "packages" => "پکیج‌ها",
            "package" => "پکیج‌ها",

            // Roles
            "roles" => "نقش‌ها",
            "role" => "نقش‌ها",

            // Commission
            "commission" => "کمیسیون",
            "withdrawals" => "درخواست‌های برداشت",
            "withdrawal-reports" => "گزارش برداشت‌ها",
            "weekly-reports" => "گزارش هفتگی",
            "payouts" => "واریزی‌ها",

            // Payment
            "payment" => "پرداخت",
            "transactions" => "تراکنش‌ها",
            "manual-payments" => "پرداخت دستی",

            // Network
            "network" => "شبکه",
            "tree" => "درخت شبکه",
            "balances" => "مانده‌ها",
            "statistics" => "آمار",
            "user-network" => "شبکه کاربر",

            // Club
            "club" => "باشگاه",
            "members" => "اعضا",
            "features" => "فیچرها",

            // Wallet
            "wallets" => "کیف‌پول کاربران",

            // Contracts
            "contracts" => "قراردادها",

            // Blog
            "blog" => "بلاگ",
            "posts" => "پست‌ها",

            // Content
            "content" => "محتوا",
            "pages" => "صفحات سایت",
            "public-messages" => "پیام‌های عمومی",

            // Discount Shop
            "discount" => "فروشگاه تخفیفی",
            "discount-products" => "محصولات تخفیفی",
            "discount-categories" => "دسته‌بندی تخفیفی",
            "discount-orders" => "سفارش‌های تخفیفی",
            "sales-reports" => "گزارش فروش",

            // Inventory
            "inventory" => "انبار",
            "warehouses" => "انبارها",
            "movements" => "جابه‌جایی‌ها",
            "low-stock" => "کمبود موجودی",

            // System
            "system" => "سیستم",
            "configuration" => "تنظیمات",
            "health" => "سلامت سیستم",
            "worker-control" => "کنترل وظایف",
            "alerts" => "هشدارها",
            "app-versions" => "نسخه‌های اپ",

            // Settings
            "settings" => "تنظیمات",
            "tags" => "تگ‌ها",

            // Numeric IDs - skip
            _ when long.TryParse(segment, out _) => "",
            _ => ""
        };
    }
}
