import { commanderBriefDiff } from "./src/gh";

type JsonRecord = Record<string, unknown>;

function assertCondition(condition: unknown, message: string, detail: unknown = {}): void {
  if (!condition) throw new Error(`${message}: ${JSON.stringify(detail)}`);
}

export function runGhCommanderBriefContract(): JsonRecord {
  const oldBody = [
    "# 指挥简报",
    "",
    "## 常驻观察与长期建议",
    "",
    "- 保持队列监督。",
    "",
    "## 更新 2026-05-20 17:28 北京时间",
    "",
    "- 已完成初始观察。",
    "",
  ].join("\n");
  const newSection = [
    "## 更新 2026-05-20 18:05 北京时间",
    "",
    "- 新增进展包含 `code`。",
    "",
    "| 项 | 状态 |",
    "| --- | --- |",
    "| GitHub | ready |",
    "",
    "真实换行必须保留。",
  ].join("\n");

  const appended = commanderBriefDiff(oldBody, `${oldBody}${newSection}\n`);
  assertCondition(appended.ok === true, "append-only update should be detected", appended);
  assertCondition(appended.mode === "append-only", "append-only mode should be reported", appended);
  assertCondition(appended.sectionCount === 1, "one appended section should be extracted", appended);
  assertCondition(appended.message === newSection, "appended section text should be exact", { message: appended.message });
  assertCondition(appended.message.includes("\n| 项 | 状态 |"), "markdown table should keep real newline", { message: appended.message });
  assertCondition(appended.message.includes("`code`"), "backticks should be preserved", { message: appended.message });
  assertCondition(!appended.message.includes("\\n"), "real newlines must not become literal backslash-n", { message: appended.message });

  const identical = commanderBriefDiff(oldBody, oldBody);
  assertCondition(identical.ok === false, "identical body should skip", identical);
  assertCondition(identical.mode === "identical", "identical mode should be reported", identical);
  assertCondition(String(identical.skippedReason ?? "").length > 0, "identical skip reason should be present", identical);

  const headerOnly = commanderBriefDiff(oldBody, oldBody.replace("- 保持队列监督。", "- 保持队列监督，并记录阻塞。"));
  assertCondition(headerOnly.ok === false, "header-only modification should skip", headerOnly);
  assertCondition(headerOnly.mode === "heading-diff", "header-only modification should use heading diff mode", headerOnly);

  const reordered = commanderBriefDiff(
    oldBody,
    [
      "# 指挥简报",
      "",
      "## 常驻观察与长期建议",
      "",
      "- 保持队列监督。",
      "",
      newSection,
      "",
      "## 更新 2026-05-20 17:28 北京时间",
      "",
      "- 已完成初始观察。",
      "",
    ].join("\n"),
  );
  assertCondition(reordered.ok === true, "non append-only new heading should be detected", reordered);
  assertCondition(reordered.mode === "heading-diff", "non append-only new heading should use heading-diff", reordered);
  assertCondition(reordered.message === newSection, "heading diff should return only new section", { message: reordered.message });

  return {
    ok: true,
    checks: [
      "append-only commander brief section extraction",
      "identical body skip",
      "header-only long-term observation edit skip",
      "non append-only heading diff extraction",
      "backticks, markdown table, and real newlines preserved",
    ],
  };
}

if (import.meta.main) {
  const result = runGhCommanderBriefContract();
  process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
}
