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

function readRequiredFile(file) {
  const path = resolve(process.cwd(), file);
  if (!existsSync(path)) {
    throw new Error(`Missing required file: ${file}`);
  }
  return readFileSync(path, "utf8");
}

function assertIncludes(source, snippet, label) {
  if (!source.includes(snippet)) {
    throw new Error(`${label} missing required snippet: ${snippet}`);
  }
}

const api = readRequiredFile("src/services/api.js");
const sales = readRequiredFile("src/views/SalesPlanningView.vue");
const purchase = readRequiredFile("src/views/PurchaseOrderView.vue");

assertIncludes(api, "export async function postDownloadResource", "api.js");
assertIncludes(api, "method: \"POST\"", "postDownloadResource");
assertIncludes(api, "JSON.stringify(payload)", "postDownloadResource");

for (const snippet of [
  "DocumentPaper",
  "DocumentGrid",
  "DocumentLineTable",
  "DocumentArchiveActions",
  "salesDocumentHeaderFields",
  "salesDocumentLineColumns",
  "title=\"销售订单\"",
  "销售确认",
  "客户确认",
  "财务复核",
  "制单人",
  "保存并生成PDF归档",
  "保存并归档中...",
  "previewDocumentArchive",
  "downloadDocumentArchive",
  "regenerateDocumentArchive",
  "batchDownloadSalesArchives",
  "postDownloadResource",
  "archive_status",
  "sales-archive-cell",
  "sales-archive-actions-compact",
  "sales-order",
  "归档"
]) {
  assertIncludes(sales, snippet, "SalesPlanningView.vue");
}

for (const snippet of [
  "openResource(`/document-archives/${typeKey}/${businessId}/latest/preview`)",
  "downloadResource(`/document-archives/${typeKey}/${businessId}/latest/download`",
  "postResource(`/document-archives/${typeKey}/${businessId}/generate`, {})",
  "postDownloadResource(\"/document-archives/batch-download\"",
  "document_type: \"sales-order\""
]) {
  assertIncludes(sales, snippet, "Sales archive actions");
}

for (const snippet of [
  "DocumentPaper",
  "DocumentGrid",
  "DocumentLineTable",
  "DocumentArchiveActions",
  "purchaseDocumentHeaderFields",
  "purchaseDocumentLineColumns",
  "title=\"采购订单\"",
  "采购确认",
  "供应商确认",
  "仓库接收",
  "制单人",
  "保存并生成PDF归档",
  "更新并生成PDF归档",
  "保存并归档中...",
  "previewDocumentArchive",
  "downloadDocumentArchive",
  "regenerateDocumentArchive",
  "batchDownloadPurchaseArchives",
  "postDownloadResource",
  "archive_status",
  "purchase-archive-cell",
  "purchase-archive-actions-compact",
  "purchase-order",
  "归档"
]) {
  assertIncludes(purchase, snippet, "PurchaseOrderView.vue Task 8 assertions");
}

for (const snippet of [
  "openResource(`/document-archives/${typeKey}/${businessId}/latest/preview`)",
  "downloadResource(`/document-archives/${typeKey}/${businessId}/latest/download`",
  "postResource(`/document-archives/${typeKey}/${businessId}/generate`, {})",
  "postDownloadResource(\"/document-archives/batch-download\"",
  "document_type: \"purchase-order\""
]) {
  assertIncludes(purchase, snippet, "Purchase archive actions");
}

console.log("sales/purchase document archive UI static tests passed");
