// B32 小红书 8 张卡片渲染器 — 2160×2880 @2x, deviceScaleFactor=2
// v6a-center-fill 居中铺满 1080（小红书 3:4 画布 1080×1440）
// + 末尾 Compress-Archive 打包 xiaohongshu.zip（B29 拍板硬门控）
const { chromium } = require('C:\\Users\\Rus\\.npm-global\\node_modules\\playwright');
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');

const cards = [
  { html: '01-cover.html',     png: '01-cover.png' },
  { html: '02-dichotomy.html', png: '02-dichotomy.png' },
  { html: '03-data.html',      png: '03-data.png' },
  { html: '04-step1.html',     png: '04-step1.png' },
  { html: '05-step2.html',     png: '05-step2.png' },
  { html: '06-step3.html',     png: '06-step3.png' },
  { html: '07-quote.html',     png: '07-quote.png' },
  { html: '08-cta.html',       png: '08-cta.png' },
];

(async () => {
  const browser = await chromium.launch({
    headless: true,
    args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
  });

  let totalBytes = 0;
  const cardBytes = {};
  const dir = __dirname;
  for (const c of cards) {
    const htmlPath = path.join(dir, c.html);
    const pngPath = path.join(dir, c.png);
    const context = await browser.newContext({
      viewport: { width: 1080, height: 1440 },
      deviceScaleFactor: 2,  // 强制 @2x 高清（2160×2880 物理像素）
    });
    const page = await context.newPage();
    await page.goto('file://' + htmlPath.replace(/\\/g, '/'), { waitUntil: 'networkidle' });
    await page.screenshot({ path: pngPath, type: 'png', fullPage: false, clip: { x: 0, y: 0, width: 1080, height: 1440 } });
    await context.close();
    const stats = fs.statSync(pngPath);
    totalBytes += stats.size;
    cardBytes[c.png] = stats.size;
    console.log(`✅ ${c.png}  ${stats.size} B  (${(stats.size/1024).toFixed(1)} KB)`);
  }

  await browser.close();
  console.log(`\n📦 总大小: ${(totalBytes/1024).toFixed(1)} KB (${cards.length} 张)`);
  console.log(`📐 物理尺寸: 2160×2880 (deviceScaleFactor=2 @2x)`);
  console.log(`\nJSON: ${JSON.stringify(cardBytes)}`);

  // 6. 打包 xiaohongshu.zip（8 PNG + copy.txt + title.txt + k-match.md + gen-cards.js + progress.json）
  //    SKILL.md §三 sub-3 Zip 打包硬门控（2026-06-12 B29 拍板）
  const zipPath = path.join(dir, '..', 'xiaohongshu.zip');
  const pngGlob = path.join(dir, '*.png').replace(/\\/g, '\\\\');
  const copyTxt = path.join(dir, 'copy.txt').replace(/\\/g, '\\\\');
  const titleTxt = path.join(dir, 'title.txt').replace(/\\/g, '\\\\');
  const kMatch = path.join(dir, 'k-match.md').replace(/\\/g, '\\\\');
  const genCards = path.join(dir, 'gen-cards.js').replace(/\\/g, '\\\\');
  const progressJson = path.join(dir, '..', 'progress.json').replace(/\\/g, '\\\\');
  const psCmd = `powershell -NoProfile -Command "Compress-Archive -Path '${pngGlob}','${copyTxt}','${titleTxt}','${kMatch}','${genCards}','${progressJson}' -DestinationPath '${zipPath}' -Force"`;
  console.log(`\n📦 打包 xiaohongshu.zip: ${zipPath}`);
  execSync(psCmd, { stdio: 'inherit' });
  const zipStats = fs.statSync(zipPath);
  console.log(`✅ xiaohongshu.zip 已打包: ${(zipStats.size/1024).toFixed(1)} KB`);
})();
