import { chromium } from 'playwright';
import { spawn } from 'child_process';
const PORT = 4220;
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={};
const getActive = (page) => page.evaluate(() => window.game.scene.scenes.filter(s=>s.scene.isActive()).map(s=>s.scene.key));
async function walk(page, dir, ms){ await page.keyboard.down(dir); await page.waitForTimeout(ms); await page.keyboard.up(dir); await page.waitForTimeout(120); }
async function walkToScene(page, dir, wantScene, maxSteps=25){
  await page.keyboard.down(dir);
  for (let i=0;i<maxSteps;i++){ await page.waitForTimeout(220); const a=await getActive(page); if(a.includes(wantScene)){ await page.keyboard.up(dir); return true; } }
  await page.keyboard.up(dir); return false;
}
async function enterPlaza(page){
  await page.keyboard.down('s');
  for (let i=0;i<15;i++){ await page.waitForTimeout(220); const a=await getActive(page); if(a.includes('PlazaScene')) break; }
  await page.keyboard.up('s'); await page.waitForTimeout(700);
}
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(1200);
  for (let i=0;i<4;i++){ await page.keyboard.press('e'); await page.waitForTimeout(120); }
  await page.click('canvas'); await page.waitForTimeout(300);

  // Test each plaza exit independently by teleporting to plaza center first
  await enterPlaza(page);
  // bakery: walk up to row ~8 then left
  await walk(page, 'w', 2500);
  checks.bakery = await walkToScene(page, 'a', 'BakeryScene');
  await page.waitForTimeout(500);

  // reset: go back to plaza
  if (!checks.bakery) await page.evaluate(() => { const g=window.game; g.scene.stop('HouseScene'); g.scene.start('PlazaScene',{spawn:'default'}); });
  else await walkToScene(page, 's', 'PlazaScene');
  await page.waitForTimeout(700); await page.click('canvas');

  // shop: walk up then right
  await walk(page, 'w', 2500);
  checks.shop = await walkToScene(page, 'd', 'ShopScene');
  await page.waitForTimeout(500);
  await walkToScene(page, 's', 'PlazaScene'); await page.waitForTimeout(700); await page.click('canvas');

  // clinic: walk up
  await walk(page, 'a', 600);
  checks.clinic = await walkToScene(page, 'w', 'ClinicScene');
  await page.waitForTimeout(500);
  await walkToScene(page, 's', 'PlazaScene'); await page.waitForTimeout(700); await page.click('canvas');

  // lake: walk down then left (lake at col0 row12 = lower left)
  await walk(page, 's', 1500);
  checks.lake = await walkToScene(page, 'a', 'LakeScene');
  await page.waitForTimeout(500);

  await browser.close();
} catch(e){ console.log('FAIL', e.message); } finally { server.kill('SIGTERM'); }
console.log('CHECKS:', JSON.stringify(checks, null, 2));
