import fs from "node:fs";
import path from "node:path";

const root = process.cwd();
const inventoryPath = path.join(root, "src/views/InventoryLedgerView.vue");
const appPath = path.join(root, "src/App.vue");
const routerPath = path.join(root, "src/router/index.js");
const deliveryWorkspacePath = path.join(root, "src/views/DeliveryWorkspaceView.vue");
const dictionariesPath = path.join(root, "src/utils/dictionaries.js");

const inventory = fs.readFileSync(inventoryPath, "utf8");
const app = fs.readFileSync(appPath, "utf8");
const router = fs.readFileSync(routerPath, "utf8");
const deliveryWorkspace = fs.readFileSync(deliveryWorkspacePath, "utf8");
const dictionaries = fs.readFileSync(dictionariesPath, "utf8");

const checks = [
  ["return warehouse tab", inventory.includes('key: "return"') && inventory.includes('label: "退货库"')],
  ["return warehouse type mapping", inventory.includes('return: "RETURN"') && inventory.includes('return: "退货库"')],
  ["return inbound operation", inventory.includes('RETURN_IN') && inventory.includes('returnInboundDrawerOpen') && inventory.includes('/returns/orders')],
  ["return scrap inbound operation", inventory.includes('RETURN_SCRAP_IN') && inventory.includes('returnScrapInboundDrawerOpen') && inventory.includes('/returns/scrap-inbound')],
  ["return rework outbound operation", inventory.includes('RETURN_OUT') && inventory.includes('returnReworkDrawerOpen') && inventory.includes('/returns/rework-outbound')],
  ["delivery-line trace loaded", inventory.includes('/sales/delivery-items?limit=500') && inventory.includes('formatReturnTraceSummary')],
  ["return items loaded", inventory.includes('/returns/items?limit=500') && inventory.includes('pendingReturnItems')],
  ["old delivery return child hidden", !app.includes('label: "退货处理"') && !deliveryWorkspace.includes('ReturnManagementView')],
  ["old route redirects to warehouse", router.includes('path: "/return-management"') && router.includes('redirect: { name: "inventory-ledger" }')],
  ["dictionary labels", dictionaries.includes('RETURN_IN: "退货入库"') && dictionaries.includes('RETURN_SCRAP_IN: "退货废料入库"') && dictionaries.includes('RETURN_OUT: "返工出库"')]
];

const failed = checks.filter(([, passed]) => !passed);
if (failed.length) {
  console.error("Return warehouse UI checks failed:");
  failed.forEach(([name]) => console.error(`- ${name}`));
  process.exit(1);
}

console.log("Return warehouse UI checks passed");
