/** Dev helper: drive the game headless and dump screenshots to /tmp. */
import { chromium, devices } from '@playwright/test';

async function main() {
  const browser = await chromium.launch();
  const ctx = await browser.newContext({
    ...devices['iPhone 12'],
    viewport: { width: 390, height: 844 },
  });
  const page = await ctx.newPage();
  const errors: string[] = [];
  page.on('pageerror', (e) => errors.push(`pageerror: ${e.message}`));
  page.on('console', (m) => {
    if (m.type() === 'error') errors.push(`console: ${m.text()}`);
  });
  await page.goto('http://localhost:4173/');
  await page.waitForFunction(() => (window as any).__game !== undefined);
  await page.tap('#ov-btn');
  await page.waitForTimeout(500);
  await page.screenshot({ path: '/tmp/hullrot-spawn.png' });

  // muzzle flash frame
  await page.keyboard.press('Space');
  await page.waitForTimeout(50);
  await page.screenshot({ path: '/tmp/hullrot-flash.png' });

  // east cable room with stalker
  await page.evaluate(() => (window as any).__game.teleport(17.5, 12.5, 0.4));
  await page.waitForTimeout(400);
  await page.screenshot({ path: '/tmp/hullrot-east.png' });

  // hurt the player for face/vignette check
  await page.evaluate(() => (window as any).__game.teleport(11.5, 7.2, Math.PI / 2));
  await page.waitForTimeout(2600);
  await page.screenshot({ path: '/tmp/hullrot-hurt.png' });

  const g = await page.evaluate(() => {
    const w = window as any;
    return { phase: w.__game.phase, hp: w.__game.hp, ammo: w.__game.ammo, enemies: w.__game.enemies };
  });
  console.log('state:', JSON.stringify(g));
  console.log(errors.length ? `ERRORS:\n${errors.join('\n')}` : 'no console errors');
  await browser.close();
}

main();
