import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

function readSource(relativePath) {
  return fs.readFileSync(path.resolve(relativePath), "utf8");
}

function assertContains(source, expected, message) {
  assert.ok(source.includes(expected), message);
}

function assertNotContains(source, unexpected, message) {
  assert.ok(!source.includes(unexpected), message);
}

const serviceSource = readSource("src/services/employeeOptions.js");

assertContains(
  serviceSource,
  "PERSONNEL_PERMISSION_CODES",
  "employee option service should define PERSONNEL_PERMISSION_CODES"
);

assertContains(
  serviceSource,
  "/master-data/employee-options?",
  "employee option service should call the permission-filtered employee options endpoint"
);

const salesSource = readSource("src/views/SalesPlanningView.vue");

assertContains(salesSource, "销售人员", "sales order form should include a sales employee label");
assertContains(salesSource, "sales_employee_id", "sales order form should submit sales_employee_id");

const permissionSelectorViews = [
  {
    path: "src/views/PurchaseOrderView.vue",
    constant: "PURCHASE_ORDER"
  },
  {
    path: "src/views/PurchaseReceiptView.vue",
    constant: "PURCHASE_RECEIPT"
  },
  {
    path: "src/views/InventoryLedgerView.vue",
    constant: "INVENTORY_LEDGER"
  },
  {
    path: "src/views/CompletionReceiptView.vue",
    constant: "INVENTORY_LEDGER"
  },
  {
    path: "src/views/ReturnManagementView.vue",
    constant: "INVENTORY_LEDGER"
  },
  {
    path: "src/views/EquipmentManagementView.vue",
    constant: "EQUIPMENT_MANAGEMENT"
  }
];

for (const view of permissionSelectorViews) {
  const source = readSource(view.path);
  assertContains(
    source,
    "fetchPermissionEmployeeOptions",
    `${view.path} should load personnel options through fetchPermissionEmployeeOptions`
  );
  assertContains(
    source,
    `PERSONNEL_PERMISSION_CODES.${view.constant}`,
    `${view.path} should use PERSONNEL_PERMISSION_CODES.${view.constant}`
  );
  assertNotContains(
    source,
    "/master-data/employees",
    `${view.path} should not call the unrestricted employee endpoint`
  );
}

console.log("permission employee selector checks passed");
