import { BreadcrumbItemType } from "@/Components/shared/BreadcrumbNav";

/**
 * 麵包屑定義對應表
 * 根據側邊欄層級結構定義基礎麵包屑
 */
export const BREADCRUMB_MAP: Record<string, BreadcrumbItemType[]> = {
    dashboard: [],
    products: [
        { label: "商品與庫存管理" },
        { label: "商品資料管理", href: "/products", isPage: true }
    ],
    warehouses: [
        { label: "商品與庫存管理" },
        { label: "倉庫管理", href: "/warehouses", isPage: true }
    ],
    vendors: [
        { label: "供應鏈管理", href: '#' },
        { label: "廠商資料管理", href: "/vendors", isPage: true }
    ],
    purchaseOrders: [
        { label: "供應鏈管理", href: '#' },
        { label: "採購單管理", href: "/purchase-orders", isPage: true }
    ],
    productionOrders: [
        { label: "生產管理" },
        { label: "生產工單", href: "/production-orders", isPage: true }
    ],
    productionOrdersCreate: [
        { label: "生產管理" },
        { label: "生產工單", href: "/production-orders" },
        { label: "建立生產單", isPage: true }
    ],
    productionOrdersShow: [
        { label: "生產管理" },
        { label: "生產工單", href: "/production-orders" },
        { label: "詳情", isPage: true }
    ],
    recipes: [
        { label: "生產管理" },
        { label: "配方管理", href: "/recipes", isPage: true }
    ],
};

/**
 * 組合麵包屑工具
 * @param base 基礎路徑名稱 (key of BREADCRUMB_MAP)
 * @param extra 額外的路徑項目 (例如編輯、詳情頁)
 */
export function getBreadcrumbs(base: keyof typeof BREADCRUMB_MAP, extra?: BreadcrumbItemType[]): BreadcrumbItemType[] {
    const baseItems = JSON.parse(JSON.stringify(BREADCRUMB_MAP[base] || []));

    if (extra && extra.length > 0) {
        // 如果有額外路徑，基礎路徑的最後一項不應標記為 isPage
        if (baseItems.length > 0) {
            baseItems[baseItems.length - 1].isPage = false;
        }
        return [...baseItems, ...extra];
    }

    return baseItems;
}

/**
 * 取得「新增」操作的麵包屑
 */
export function getCreateBreadcrumbs(base: keyof typeof BREADCRUMB_MAP): BreadcrumbItemType[] {
    return getBreadcrumbs(base, [{ label: "新增", isPage: true }]);
}

/**
 * 取得「編輯」操作的麵包屑
 */
export function getEditBreadcrumbs(base: keyof typeof BREADCRUMB_MAP): BreadcrumbItemType[] {
    return getBreadcrumbs(base, [{ label: "編輯", isPage: true }]);
}

/**
 * 取得「詳情」操作的麵包屑
 */
export function getShowBreadcrumbs(base: keyof typeof BREADCRUMB_MAP, suffix: string = "詳情"): BreadcrumbItemType[] {
    return getBreadcrumbs(base, [{ label: suffix, isPage: true }]);
}

/**
 * 取得「庫存管理」子頁面的麵包屑
 * 層級：倉庫管理 > 庫存詳情 (倉庫名) > [功能]
 */
export function getInventoryBreadcrumbs(warehouseId: string | number, warehouseName: string, subPageLabel?: string): BreadcrumbItemType[] {
    const baseItems: BreadcrumbItemType[] = [
        ...JSON.parse(JSON.stringify(BREADCRUMB_MAP.warehouses || []))
    ];

    // 修改「倉庫管理」不作為最後一頁
    if (baseItems.length > 0) {
        baseItems[baseItems.length - 1].isPage = false;
    }

    const inventoryDetailItem: BreadcrumbItemType = {
        label: `庫存管理 (${warehouseName})`,
        href: `/warehouses/${warehouseId}/inventory`,
        isPage: !subPageLabel
    };

    const finalItems = [...baseItems, inventoryDetailItem];

    if (subPageLabel) {
        finalItems.push({ label: subPageLabel, isPage: true });
    }

    return finalItems;
}
