import { chromium } from 'playwright';
import { spawn } from 'child_process';
const PORT = 4194;
const server = spawn('node', ['node_modules/vite/bin/vite.js', 'preview', '--port', String(PORT), '--strictPort'], { cwd: process.cwd(), stdio: 'pipe' });
const waitServer = () => new Promise((res, rej) => { const t=setTimeout(()=>rej('timeout'),30000); server.stdout.on('data',d=>{if(d.toString().includes('Local:')){clearTimeout(t);res();}}); server.stderr.on('data',d=>{if(d.toString().includes('Local:')){clearTimeout(t);res();}});});
const checks = {};
try {
  await waitServer();
  const browser = await chromium.launch();
  const page = await browser.newPage({ viewport: { width: 960, height: 540 } });
  page.on('pageerror', (e) => console.log('PAGEERR', e.message));
  await page.goto(`http://localhost:${PORT}/`, { waitUntil: 'load', timeout: 15000 });
  await page.waitForTimeout(800);
  await page.keyboard.press('Enter');
  await page.waitForTimeout(1000);
  // discover C1 (calendar) - need day>=2
  await page.evaluate(() => window.director.debugSetDay(2));
  const before = await page.evaluate(() => ({ aware: window.director.awareness, clues: window.director.clues.size }));
  // discover 3 clues
  await page.evaluate(() => { window.director.discoverClue('C1'); window.director.discoverClue('C2'); window.director.discoverClue('C3'); });
  const after = await page.evaluate(() => ({ aware: window.director.awareness, clues: window.director.clues.size }));
  checks.clue_aware_24 = after.aware === 24; // 3*8
  checks.clue_count_3 = after.clues === 3;
  // sadness behavior: -5 happy +3 aware
  const hb = await page.evaluate(() => window.director.happiness);
  await page.evaluate(() => window.director.sadness('test'));
  const ha = await page.evaluate(() => ({ happy: window.director.happiness, aware: window.director.awareness }));
  checks.sadness_happy_minus5 = ha.happy === hb - 5;
  checks.sadness_aware_plus3 = ha.aware === after.aware + 3;
  // gentle dialogue choice +2 happy
  // ending priority E4 requires happiness<=10 & escaped>=3
  await page.evaluate(() => { window.director.debugSetHappiness(5); window.director.escapedFestivals = 3; window.director.debugSetDay(6); });
  const e4 = await page.evaluate(() => window.director.determineEnding());
  checks.ending_E4 = e4 === 'E4';
  // E1: awareness<=29 & happiness>=90
  await page.evaluate(() => { window.director.debugSetAwareness(10); window.director.debugSetHappiness(95); });
  const e1 = await page.evaluate(() => window.director.determineEnding());
  checks.ending_E1 = e1 === 'E1';
  // E2: awareness 30-69
  await page.evaluate(() => { window.director.debugSetAwareness(50); window.director.debugSetHappiness(50); });
  const e2 = await page.evaluate(() => window.director.determineEnding());
  checks.ending_E2 = e2 === 'E2';
  await browser.close();
} catch(e){ console.log('FAIL', e.message); } finally { server.kill('SIGTERM'); }
console.log('CHECKS:', JSON.stringify(checks));
process.exit(Object.values(checks).every(v=>v) ? 0 : 1);
