const { chromium } = require('playwright');
const path = require('path');

(async () => {
  const file = path.resolve(__dirname, '..', 'deliverables/commercial_v07/overview_v07.html');
  const out = path.resolve(__dirname, '..', 'deliverables/commercial_v07/渚咖咖啡杯_5张主图_v07_总览.jpg');
  const browser = await chromium.launch({ channel: 'chrome', headless: true });
  const page = await browser.newPage({ viewport: { width: 900, height: 680 }, deviceScaleFactor: 1 });
  await page.goto('file://' + file, { waitUntil: 'load' });
  await page.waitForLoadState('networkidle');
  await page.evaluate(async () => {
    await Promise.all(Array.from(document.images).map(img => img.complete ? true : new Promise(resolve => { img.onload = img.onerror = resolve; })));
  });
  const broken = await page.evaluate(() => Array.from(document.images).filter(i => !i.naturalWidth).map(i => i.getAttribute('src')));
  if (broken.length) throw new Error('Broken overview images: ' + broken.join(', '));
  await page.locator('#overview').screenshot({ path: out, type: 'jpeg', quality: 92 });
  await browser.close();
  console.log(out);
})();
