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

const template = path.join(__dirname, '..', 'views', 'listing.ejs');
const languages = ['ko', 'en', 'zh', 'ja'];

const baseModel = {
  isMainnet: true,
  chainName: 'PLAIS Chain',
  networkId: 'plais-qbft-20260718',
  chainId: 20260718,
  consensus: 'QBFT',
  blockTimeTargetSec: 2,
  launchControlStatus: 'AWAITING_EXPLICIT_LAUNCH_APPROVAL',
  externalTransfersLocked: true,
  symbol: 'PLAIS',
  rewardUnit: 'plais Point',
  addressPrefix: '0x',
  totalSupply: '10,000,000,000',
  readinessScore: 100,
  chainStatus: 'OK',
  latestHeight: 620612,
  walletCount: 0,
  nodeCount: 4,
  onlineNodeCount: 4,
  validatorCount: 4,
  finalizedBlocks: 620613,
  circulating: 0,
  rewardSummary: { net: 0, count: 0 },
  sourceManifest: {
    ok: true,
    version: 'test-mainnet-source',
    sha256: 'a'.repeat(64),
    fileCount: 259,
    generatedAt: '2026-08-04T00:00:00.000Z'
  },
  formatNumber(value, digits = 2) {
    return Number(value || 0).toLocaleString('en-US', { maximumFractionDigits: digits });
  },
  chainLangUrl(lang) {
    return `/listing?lang=${lang}`;
  }
};

async function main() {
  const checks = [];
  for (const lang of languages) {
    const html = await ejs.renderFile(template, { ...baseModel, chainLang: lang });
    checks.push({ key: `${lang}:renders`, ok: html.length > 1000 });
    checks.push({ key: `${lang}:chain-id`, ok: html.includes('20260718') });
    checks.push({ key: `${lang}:consensus`, ok: html.includes('QBFT') });
    checks.push({ key: `${lang}:canonical-mainnet-label`, ok: html.includes('Canonical Mainnet') && html.includes('PLAIS Chain') && !html.includes('PLAIS Chain Candidate') });
    checks.push({ key: `${lang}:public-activation-locked`, ok: html.includes('LOCKED') });
    checks.push({ key: `${lang}:launch-control`, ok: html.includes('AWAITING_EXPLICIT_LAUNCH_APPROVAL') });
    checks.push({ key: `${lang}:legacy-reward-unit`, ok: html.includes('plais Point') });
    checks.push({ key: `${lang}:no-conversion-claim`, ok: !/can currently request conversion|현재 1,000P = 1 plais 기준으로 전환 요청|目前可按 1,000P|現在 1,000P/.test(html) });
    checks.push({ key: `${lang}:current-source`, ok: html.includes('test-mainnet-source') });
  }

  const failed = checks.filter((check) => !check.ok);
  const report = {
    ok: failed.length === 0,
    schemaVersion: 'plais-listing-materials-self-test/v1',
    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;
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
