import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import path from "node:path";

const root = process.cwd();

function readSource(relativePath) {
  return readFileSync(path.join(root, relativePath), "utf8");
}

const router = readSource("src/router/index.js");
const app = readSource("src/App.vue");
const productionLedgerView = readSource("src/views/ProductionLedgerView.vue");
const inventoryView = readSource("src/views/InventoryLedgerView.vue");

assert.match(
  router,
  /import\s+ProductionLedgerView\s+from\s+"..\/views\/ProductionLedgerView\.vue";/,
  "router should import ProductionLedgerView"
);
assert.match(
  router,
  /path:\s*"\/production-ledger"[\s\S]*?name:\s*"production-ledger"[\s\S]*?component:\s*ProductionLedgerView[\s\S]*?permission:\s*"MENU_WORK_ORDER"/,
  "router should define production-ledger route with MENU_WORK_ORDER permission"
);
assert.match(
  router,
  /path:\s*"\/work-order-ledger"[\s\S]*?redirect:\s*\{\s*name:\s*"production-ledger"\s*\}/,
  "legacy work-order-ledger route should redirect to production-ledger"
);
assert.match(
  app,
  /label:\s*"生产台账"[\s\S]*?to:\s*\{\s*name:\s*"production-ledger"\s*\}[\s\S]*?permission:\s*"MENU_WORK_ORDER"/,
  "App.vue production menu should expose 生产台账 linked to production-ledger"
);

assert.match(
  productionLedgerView,
  /fetchResource\("\/production\/production-ledger\?limit=500"/,
  "ProductionLedgerView should load the production ledger endpoint with limit=500"
);
assert.match(productionLedgerView, /材料库存批次号/, "ProductionLedgerView should show material stock lot number");
assert.match(productionLedgerView, /库外未闭环重量/, "ProductionLedgerView should show outside unclosed weight");
assert.match(productionLedgerView, /该批材料结单/, "ProductionLedgerView should provide material batch lock action");
assert.doesNotMatch(productionLedgerView, /容差上限/, "ProductionLedgerView should not expose tolerance limit wording to users");
assert.doesNotMatch(inventoryView, /容差上限/, "InventoryLedgerView should not expose tolerance limit wording to users");
assert.match(inventoryView, /该批材料余料结单/, "InventoryLedgerView should provide a production-surplus material batch settle option");
assert.match(inventoryView, /该批材料废料结单/, "InventoryLedgerView should provide a production-scrap material batch settle option");
assert.match(
  inventoryView,
  /source_doc_type:\s*isProductionSurplusInbound\.value\s*\?\s*"PRODUCTION_LEDGER"/,
  "InventoryLedgerView production surplus inbound payload should link production ledger source document"
);
assert.match(
  inventoryView,
  /source_doc_type:\s*isProductionScrapInbound\.value\s*\?\s*"PRODUCTION_LEDGER"/,
  "InventoryLedgerView production scrap inbound payload should link production ledger source document"
);

console.log("production ledger static checks passed");
