import puppeteer from 'puppeteer';
const OUT=process.env.OUT, URL=process.env.URL, TAG=process.env.TAG||'live';
const b=await puppeteer.launch({executablePath:'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',headless:'new',args:['--no-sandbox']});
for (const [name,w,h] of [['phone',390,844],['desktop',1440,900]]){
  const p=await b.newPage();
  p.on('pageerror',e=>console.log(`  [${name} pageerror]`,String(e).slice(0,120)));
  p.on('console',m=>{if(m.type()==='error')console.log(`  [${name} console]`,m.text().slice(0,120));});
  await p.setViewport({width:w,height:h,deviceScaleFactor:2});
  await p.goto(URL,{waitUntil:'networkidle2',timeout:45000});
  await new Promise(r=>setTimeout(r,2500));
  await p.screenshot({path:`${OUT}/${TAG}-${name}.png`,fullPage:true});
  const info=await p.evaluate(()=>({title:document.title,h:document.body.scrollHeight,
    text:document.body.innerText.replace(/\s+/g,' ').slice(0,300)}));
  console.log(`  ${name}: ${info.title} | height ${info.h}px`);
  console.log(`     ${info.text}`);
  await p.close();
}
await b.close();
