import { chromium } from 'playwright';
import { spawn } from 'child_process';
const PORT = 4216;
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();}});});
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(150); }
  await page.click('canvas'); await page.waitForTimeout(300);

  // house -> plaza
  await page.keyboard.down('s');
  for (let i=0;i<15;i++){ await page.waitForTimeout(250); const a=await page.evaluate(()=>window.game.scene.scenes.filter(s=>s.scene.isActive()).map(s=>s.scene.key)); if(a.includes('PlazaScene')) break; }
  await page.keyboard.up('s'); await page.waitForTimeout(700);

  const inPlaza = await page.evaluate(() => window.game.scene.scenes.filter(s=>s.scene.isActive()).map(s=>s.scene.key));
  console.log('after entering plaza:', JSON.stringify(inPlaza));
  const pos = await page.evaluate(() => { const p=window.game.scene.getScene('PlazaScene'); return {px:p.player.x, py:p.player.y, doors:p.doors.map(d=>({x:d.x,y:d.y,t:d.target}))}; });
  console.log('player pos in plaza:', pos.px, pos.py);
  console.log('doors:', JSON.stringify(pos.doors));

  // Now walk left toward bakery door (col 0, row 8 = 0,128). Player at ~(224, 276).
  // Need to go up to row 8 first. Walk up.
  await page.keyboard.down('w'); await page.waitForTimeout(2500); await page.keyboard.up('w'); await page.waitForTimeout(200);
  const afterUp = await page.evaluate(() => { const p=window.game.scene.getScene('PlazaScene').player; return {x:p.x,y:p.y}; });
  console.log('after walking up:', JSON.stringify(afterUp));
  // now walk left
  await page.keyboard.down('a');
  for (let i=0;i<20;i++){ await page.waitForTimeout(250); const a=await page.evaluate(()=>window.game.scene.scenes.filter(s=>s.scene.isActive()).map(s=>s.scene.key)); if(a.includes('BakeryScene')){ console.log('entered bakery at step',i); break; } }
  await page.keyboard.up('a');
  const finalScenes = await page.evaluate(() => window.game.scene.scenes.filter(s=>s.scene.isActive()).map(s=>s.scene.key));
  const finalPos = await page.evaluate(() => { try { return {x:window.game.scene.getScene('PlazaScene').player.x, y:window.game.scene.getScene('PlazaScene').player.y}; } catch(e){ return 'plaza gone'; } });
  console.log('final scenes:', JSON.stringify(finalScenes), 'pos:', JSON.stringify(finalPos));

  await browser.close();
} catch(e){ console.log('FAIL', e.message); } finally { server.kill('SIGTERM'); }
