"use client";

import Breadcrumbs from "@mui/material/Breadcrumbs";
import Typography from "@mui/material/Typography";
import Link from "next/link";
import MUILink from "@mui/material/Link";
import { usePathname } from "next/navigation";
import { useTranslation } from "react-i18next";

const pathToLabelKey: { [path: string]: string } = {
  "": "nav.breadcrumb.home",
  "/chart": "nav.breadcrumb.chart",
  "/chart/warehouse": "nav.breadcrumb.chartWarehouse",
  "/chart/purchase": "nav.breadcrumb.chartPurchase",
  "/chart/delivery": "nav.breadcrumb.chartDelivery",
  "/chart/joborder": "nav.breadcrumb.chartJobOrder",
  "/chart/joborder/board": "nav.breadcrumb.chartJobOrderBoard",
  "/chart/forecast": "nav.breadcrumb.chartForecast",
  "/projects": "nav.breadcrumb.projects",
  "/projects/create": "nav.breadcrumb.projectsCreate",
  "/tasks": "nav.breadcrumb.tasks",
  "/tasks/create": "nav.breadcrumb.tasksCreate",
  "/settings/qcItem": "nav.breadcrumb.qcItem",
  "/settings/qcItemAll": "nav.breadcrumb.qcItemAll",
  "/settings/qrCodeHandle": "nav.breadcrumb.qrCodeHandle",
  "/settings/rss": "nav.breadcrumb.demandForecast",
  "/settings/equipment": "nav.breadcrumb.equipment",
  "/settings/equipment/MaintenanceEdit": "nav.breadcrumb.equipmentMaintenanceEdit",
  "/settings/shop": "nav.breadcrumb.shop",
  "/settings/shop/board": "nav.breadcrumb.routeBoard",
  "/settings/shop/detail": "nav.breadcrumb.shopDetail",
  "/settings/shop/truckdetail": "nav.breadcrumb.truckLaneDetail",
  "/settings/printer": "nav.breadcrumb.printer",
  "/scheduling/rough": "nav.breadcrumb.schedulingRough",
  "/scheduling/rough/edit": "nav.breadcrumb.schedulingRoughEdit",
  "/scheduling/detailed": "nav.breadcrumb.schedulingDetailed",
  "/scheduling/detailed/edit": "nav.breadcrumb.schedulingDetailedEdit",
  "/inventory": "nav.breadcrumb.inventory",
  "/settings/importTesting": "nav.breadcrumb.importTesting",
  "/settings/m18ImportTesting": "nav.breadcrumb.importTesting",
  "/do": "nav.deliveryOrder",
  "/doworkbench": "nav.store.doWorkbench",
  "/doworkbenchsearch": "nav.breadcrumb.doWorkbenchSearch",
  "/doworkbench/pick": "nav.breadcrumb.doWorkbenchPick",
  "/doworkbench/edit": "nav.breadcrumb.doWorkbenchEdit",
  "/pickOrder": "nav.store.pickOrder",
  "/po": "nav.store.purchaseOrder",
  "/po/edit": "nav.breadcrumb.poEdit",
  "/po/workbench": "nav.breadcrumb.poWorkbench",
  "/dashboard": "nav.dashboard",
  "/jo": "nav.jobOrder.searchCreate",
  "/jo/edit": "nav.breadcrumb.joEdit",
  "/jo/testing": "nav.breadcrumb.joTesting",
  "/jo/workbench": "nav.breadcrumb.joWorkbench",
  "/putAway": "nav.store.putAwayScan",
  "/stockIssue": "nav.store.stockIssue",
  "/stocktakemanagement": "nav.store.stockTake",
  "/stockRecord": "nav.store.stockRecord",
  "/report": "nav.breadcrumb.report",
  "/m18Syn": "nav.breadcrumb.m18Sync",
  "/bagPrint": "nav.breadcrumb.bagPrint",
  "/laserPrint": "nav.breadcrumb.laserPrint",
  "/settings/itemPrice": "nav.breadcrumb.priceInquiry",
  "/finishedGood": "nav.breadcrumb.finishedGood",
  "/finishedGood/management": "nav.breadcrumb.finishedGoodManagement",
  "/ps": "nav.scheduling",
  "/productionProcess": "nav.jobOrder.productionProcess",
  "/jodetail": "nav.jobOrder.pickExecution",
  "/bag": "nav.jobOrder.bagUsage",
};

const Breadcrumb = () => {
  const pathname = usePathname();
  const segments = pathname.split("/");
  const { t } = useTranslation(["navigation", "common"]);

  return (
    <Breadcrumbs>
      {segments.map((segment, index) => {
        const href = segments.slice(0, index + 1).join("/");
        const labelKey = pathToLabelKey[href];
        const label = labelKey
          ? t(labelKey)
          : t(segment, { ns: "common", defaultValue: segment });

        if (index === segments.length - 1) {
          return (
            <Typography key={index} color="text.primary">
              {label}
            </Typography>
          );
        } else {
          return (
            <MUILink
              underline="hover"
              color="inherit"
              key={index}
              component={Link}
              href={href || "/"}
            >
              {label}
            </MUILink>
          );
        }
      })}
    </Breadcrumbs>
  );
};

export default Breadcrumb;
