const assert = require("assert");
const fs = require("fs");
const path = require("path");

const source = fs.readFileSync(
  path.resolve(__dirname, "../../src/api/ccdi/projectOverview.js"),
  "utf8"
);

[
  "getOverviewDashboard",
  "getOverviewRiskPeople",
  "getOverviewRiskModelCards",
  "getOverviewRiskModelPeople",
  "getOverviewSuspiciousTransactions",
  "getOverviewEmployeeCreditNegative",
  "exportOverviewReport",
  "/ccdi/project/overview/dashboard",
  "/ccdi/project/overview/risk-people",
  "/ccdi/project/overview/risk-models/cards",
  "/ccdi/project/overview/risk-models/people",
  "/ccdi/project/overview/suspicious-transactions",
  "/ccdi/project/overview/employee-credit-negative",
  "/ccdi/project/overview/report/export",
].forEach((token) => assert(source.includes(token), token));

[
  "getOverviewTopRiskPeople",
  "/ccdi/project/overview/top-risk-people",
].forEach((token) => assert(!source.includes(token), token));

[
  "getOverviewRiskPeople(params)",
  "/ccdi/project/overview/risk-people",
  "projectId: params.projectId",
  "pageNum: params.pageNum",
  "pageSize: params.pageSize",
].forEach((token) => assert(source.includes(token), token));

const riskModelPeopleFn = source.match(
  /export function getOverviewRiskModelPeople\(params\) \{[\s\S]*?params:\s*\{([\s\S]*?)\}\s*\}\s*\)/m
);

assert(riskModelPeopleFn, "应保留模型人员接口参数透传逻辑");

[
  "projectId: params.projectId",
  "modelCodes: params.modelCodes",
  "matchMode: params.matchMode",
  "keyword: params.keyword",
  "deptId: params.deptId",
  "pageNum: params.pageNum",
  "pageSize: params.pageSize",
].forEach((token) => assert(riskModelPeopleFn[0].includes(token), token));

const suspiciousTransactionsFn = source.match(
  /export function getOverviewSuspiciousTransactions\(params\) \{[\s\S]*?params:\s*\{([\s\S]*?)\}\s*\}\s*\)/m
);

assert(suspiciousTransactionsFn, "应新增涉疑交易接口参数透传逻辑");

[
  "projectId: params.projectId",
  "suspiciousType: params.suspiciousType",
  "pageNum: params.pageNum",
  "pageSize: params.pageSize",
].forEach((token) => assert(suspiciousTransactionsFn[0].includes(token), token));

const employeeCreditNegativeFn = source.match(
  /export function getOverviewEmployeeCreditNegative\(params\) \{[\s\S]*?params:\s*\{([\s\S]*?)\}\s*\}\s*\)/m
);

assert(employeeCreditNegativeFn, "应新增员工负面征信接口参数透传逻辑");

[
  "projectId: params.projectId",
  "pageNum: params.pageNum",
  "pageSize: params.pageSize",
].forEach((token) => assert(employeeCreditNegativeFn[0].includes(token), token));

const overviewReportExportFn = source.match(
  /export function exportOverviewReport\(projectId\) \{[\s\S]*?params:\s*\{([\s\S]*?)\}\s*\}\s*\)/m
);

assert(overviewReportExportFn, "应新增结果总览PDF导出接口");

[
  "method: 'post'",
  "responseType: 'blob'",
  "projectId",
].forEach((token) => assert(overviewReportExportFn[0].includes(token), token));

["employeeResult", "resultTable", "overview/result"].forEach((token) =>
  assert(!source.includes(token), `前端 API 契约不应感知结果表实现：${token}`)
);
