// 기존 .do URL → Next.js 라우트 매핑
// menu_info.menu_url을 Next.js 경로로 변환

const URL_MAP: Record<string, string> = {
  // DASHBOARD
  "/dashboard/dashboard.do": "/dashboard",

  // 영업관리
  "/contractMgmt/contractDashBoard.do": "/sales/contract-dashboard",
  "/contractMgmt/supplyMngPagingList.do": "/sales/customer",
  "/contractMgmt/contractList.do": "/sales/contract",
  "/releaseMgmt/releaseMgmtList.do": "/sales/release",

  // 프로젝트관리
  "/project/wbsTemplateMngList.do": "/project/wbs-template",
  "/project/projectMgmtList.do": "/project/status",
  "/project/projectMgmtWbsList.do": "/project/progress",
  "/project/projectMgmtTotalList.do": "/project/total",

  // 제품관리
  "/productKindSpec/productKindSpecList.do": "/product/spec",
  "/partMng/partMngTempList.do": "/product/part-register",
  "/partMng/partMngList.do": "/product/part-list",
  "/partMng/searchStructureList.do": "/product/bom-register",
  "/partMng/structureAscendingList.do": "/product/bom-list",
  "/partMng/partMngChangeList.do": "/product/part-change",
  "/partMng/partMngHisList.do": "/product/design-change",

  // 구매품표준관리
  "/procurStandMgmt/mainCategoryList.do?search_category=0001668": "/procurement-std/code1",
  "/procurStandMgmt/mainCategoryList.do?search_category=0001669": "/procurement-std/code2",
  "/procurStandMgmt/mainCategoryList.do?search_category=0001670": "/procurement-std/code3",
  "/procurStandMgmt/mainCategoryList.do?search_category=0001671": "/procurement-std/code4",
  "/procurStandMgmt/mainCategoryList.do?search_category=0001672": "/procurement-std/code5",
  "/procurStandMgmt/materialCodeList.do": "/procurement-std/material-code",

  // 구매관리
  "/salesMng/salesBomReportList.do": "/purchase/bom",
  "/salesMng/salesPartChgList.do": "/purchase/design-change",
  "/salesMng/salesLongDeliveryList.do": "/purchase/stock",
  "/admin/supplyMngPagingList.do": "/purchase/supplier",
  "/salesMng/salesRequestMngRegList.do": "/purchase/request",

  // 발주관리
  "/purchaseOrder/purchaseOrderStatusByProject.do": "/order/status",
  "/purchaseOrder/purchaseOrderStatusAmountBySupply.do": "/order/amount-status",
  "/purchaseOrder/purchaseOrderList_new.do?countPerPage=": "/order/list",

  // 입고관리
  "/purchaseOrder/deliveryMngStatus.do": "/delivery/status",
  "/purchaseOrder/deliveryMngAcceptanceList.do": "/delivery/acceptance",
  "/purchaseOrder/deliveryMngInvalidList.do": "/delivery/defect",
  "/purchaseOrder/deliveryMngList_new.do": "/delivery/price",

  // 자재관리
  "/inventoryMng/inventoryMngDashList.do": "/inventory/status",
  "/inventoryMng/inventoryMngNewList.do": "/inventory/list",
  "/inventoryMng/inventoryMngInputList.do": "/inventory/input",
  "/inventoryMng/materialRequestList.do": "/inventory/request",
  "/fundMgmt/fundMgmtList.do": "/inventory/fund",

  // 생산관리
  "/productionplanning/planningdashboard.do": "/production/status",
  "/productionplanning/planningList.do": "/production/planning",
  "/productionplanning/processperformanceList.do": "/production/process",
  "/productionplanning/issuemgmtList.do": "/production/issue",
  "/productionplanning/inspectionMgmtList.do": "/production/inspection",
  "/productionplanning/releaseMgmtList.do": "/production/release",
  "/productionplanning/setupmgmtList.do": "/production/setup",

  // 작업관리
  "/productionplanning/workMgmtdashboard.do": "/work/status",
  "/productionplanning/workStatusByProjectList.do": "/work/status?tab=project",
  "/productionplanning/workStatusByImployeeList.do": "/work/status?tab=employee",
  "/productionplanning/workDiaryList.do": "/work/diary",

  // CS관리
  "/as/asList.do": "/cs/status",
  "/as/asMngList.do": "/cs/manage",
  "/chartMgmt/chartMgmt.do": "/cs/chart",

  // 투입원가관리
  "/costMgmt/costTotaltList.do": "/cost/status",
  "/costMgmt/materialCostTotaltList.do": "/cost/material",
  "/costMgmt/laborCostMgmtList.do": "/cost/labor",
  "/costMgmt/expenseDashBoard.do": "/cost/expense",

  // 자금관리
  "/supplyChainMgmt/fundPaymentMgmtList.do?menu=costMgmt": "/fund/invoice",
  "/costMgmt/expenseList.do?menu=costMgmt": "/fund/expense-form",

  // SCM
  "/supplyChainMgmt/purchaseOrderMgmtList.do": "/scm/order",
  "/supplyChainMgmt/invalidMgmtList.do": "/scm/defect",
  "/supplyChainMgmt/invoiceMgmtList.do": "/scm/invoice",
  "/supplyChainMgmt/supplyQCMgmtList.do": "/scm/quality",
  "/supplyChainMgmt/fundPaymentMgmtList.do": "/scm/payment",

  // Part 관리
  "/part/tempPartList.do": "/part/register",
};

export function mapMenuUrl(doUrl: string): string {
  if (!doUrl || doUrl.trim() === "") return "";

  // 정확한 매치
  if (URL_MAP[doUrl]) return URL_MAP[doUrl];

  // 쿼리스트링 포함 매치
  const baseUrl = doUrl.split("?")[0];
  if (URL_MAP[baseUrl]) return URL_MAP[baseUrl];

  // 쿼리스트링 포함된 키 매치
  for (const [key, value] of Object.entries(URL_MAP)) {
    if (doUrl.startsWith(key.split("?")[0]) && key.includes("?")) {
      // 쿼리 파라미터까지 비교
      if (doUrl.includes(key.split("?")[1]?.split("=")[0] || "")) {
        return value;
      }
    }
  }

  // 매핑 없는 경우 자동 변환 시도
  return doUrl
    .replace(/\.do.*$/, "")
    .replace(/([a-z])([A-Z])/g, "$1-$2")
    .toLowerCase();
}
