import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";

const files = [
  "src/components/documentForms/DocumentPaper.vue",
  "src/components/documentForms/DocumentGrid.vue",
  "src/components/documentForms/DocumentLineTable.vue",
  "src/components/documentForms/DocumentArchiveActions.vue",
  "src/styles/main.css"
];

const sources = new Map();

for (const file of files) {
  const path = resolve(process.cwd(), file);
  if (!existsSync(path)) {
    throw new Error(`Missing required file: ${file}`);
  }

  const source = readFileSync(path, "utf8");
  if (source.trim().length < 200) {
    throw new Error(`Required file has unexpectedly short content: ${file}`);
  }

  sources.set(file, source);
}

const documentPaper = sources.get("src/components/documentForms/DocumentPaper.vue");
for (const snippet of ["ERP留存联", "document-paper-clip", "document-paper-binder-hole"]) {
  if (!documentPaper.includes(snippet)) {
    throw new Error(`DocumentPaper missing required snippet: ${snippet}`);
  }
}

const archiveActions = sources.get("src/components/documentForms/DocumentArchiveActions.vue");
for (const snippet of ["预览PDF", "下载PDF", "重新生成"]) {
  if (!archiveActions.includes(snippet)) {
    throw new Error(`DocumentArchiveActions missing required label: ${snippet}`);
  }
}

for (const forbiddenSnippet of ["ghost-button", "action-button"]) {
  if (archiveActions.includes(forbiddenSnippet)) {
    throw new Error(`DocumentArchiveActions must not use global button class: ${forbiddenSnippet}`);
  }
}

for (const snippet of ["document-archive-button", "normalizedStatusType", ".trim()"]) {
  if (!archiveActions.includes(snippet)) {
    throw new Error(`DocumentArchiveActions missing archive action hardening snippet: ${snippet}`);
  }
}

const css = sources.get("src/styles/main.css");
for (const selector of [".document-paper-shell", ".document-paper-sheet", ".document-line-table"]) {
  if (!css.includes(selector)) {
    throw new Error(`main.css missing required selector: ${selector}`);
  }
}

for (const selector of [
  ".document-archive-button",
  ".document-archive-button-preview",
  ".document-archive-button-download",
  ".document-archive-button-regenerate"
]) {
  if (!css.includes(selector)) {
    throw new Error(`main.css missing document archive button selector: ${selector}`);
  }
}

console.log("document form components static tests passed");
