const fs = require('fs');
const path = require('path');

const root = path.join(__dirname, '..');

function read(relativePath) {
  return fs.readFileSync(path.join(root, relativePath), 'utf8').replace(/^\uFEFF/, '');
}

const documents = [
  {
    lang: 'en',
    markdown: 'docs/WHITEPAPER.md',
    html: 'docs/WHITEPAPER.html',
    required: [
      /# PLAIS Chain Whitepaper/,
      /Version 0\.4 — 2026-08-04/,
      /chain ID `20260718`/i,
      /four validators on four independently attested hosts/i,
      /technical mainnet gate passed 11\/11/i,
      /genesis supply: 10,000,000,000 PLAIS/i,
      /decimals: 18/i,
      /public circulating supply: 0/i,
      /external transfers: locked/i,
      /conversion to canonical PLAIS: disabled/i,
      /AWAITING_EXPLICIT_LAUNCH_APPROVAL|explicit public activation/i
    ],
    forbidden: [
      /not a public mainnet/i,
      /share one physical host/i,
      /four independently operated validator hosts are not yet verified/i,
      /Version 0\.3 — 2026-08-01/
    ]
  },
  {
    lang: 'ko',
    markdown: 'docs/WHITEPAPER_KO.md',
    html: 'docs/WHITEPAPER_KO.html',
    required: [
      /# PLAIS Chain 백서/,
      /버전 0\.4 — 2026-08-04/,
      /Chain ID `20260718`/i,
      /독립성이 증명된 호스트 4대의 validator 4개/i,
      /메인넷 기술 gate 11\/11/i,
      /Genesis supply: 10,000,000,000 PLAIS/i,
      /Decimals: 18/i,
      /공개 유통량: 0/i,
      /외부 전송: 잠금/i,
      /canonical PLAIS 전환: 비활성화/i,
      /명시적 공개 활성화/i
    ],
    forbidden: [
      /public mainnet이 아니다/i,
      /하나의 물리 호스트를 공유/i,
      /validator host 4대가 아직 검증되지 않음/i,
      /버전 0\.3 — 2026-08-01/
    ]
  }
];

const checks = [];
for (const document of documents) {
  const markdown = read(document.markdown);
  const html = read(document.html);
  for (const pattern of document.required) {
    checks.push({
      key: `${document.lang}:required:${pattern.source}`,
      ok: pattern.test(markdown),
      detail: document.markdown
    });
  }
  for (const pattern of document.forbidden) {
    checks.push({
      key: `${document.lang}:forbidden:${pattern.source}`,
      ok: !pattern.test(markdown),
      detail: document.markdown
    });
  }
  checks.push({
    key: `${document.lang}:html-version`,
    ok: html.includes('v0.4') && html.includes('2026-08-04') && html.includes('20260718'),
    detail: document.html
  });
  checks.push({
    key: `${document.lang}:html-mainnet-gate`,
    ok: html.includes('11/11'),
    detail: document.html
  });
}

const failed = checks.filter((check) => !check.ok);
const report = {
  ok: failed.length === 0,
  schemaVersion: 'plais-whitepaper-consistency/v1',
  version: '0.4',
  passed: checks.length - failed.length,
  total: checks.length,
  blockers: failed,
  checkedAt: new Date().toISOString()
};

console.log(JSON.stringify(report, null, 2));
if (!report.ok) process.exitCode = 1;
