const puppeteer = require('puppeteer');
(async () => {
  console.log('launching');
  const browser = await puppeteer.launch({ args: ['--no-sandbox','--disable-setuid-sandbox'], ignoreHTTPSErrors: true });
  const page = await browser.newPage();
  const url = 'https://localhost:3001/ketr.ketran/';
  console.log('goto', url);
  try {
    await page.goto(url, { waitUntil: 'networkidle2', timeout: 30000 });
    console.log('loaded');
  } catch (e) {
    console.error('load failed', e.message);
    await browser.close();
    process.exit(2);
  }
  await page.waitForTimeout(3000);
  const btn = await page.$x("//button[contains(., 'House Rules')]");
  if (btn && btn.length) {
    console.log('Found House Rules button, clicking');
    await btn[0].click();
    await page.waitForTimeout(500);
  } else {
    console.log('House Rules button not found by text; trying .Actions button');
    const btn2 = await page.$('.Actions button');
    if (btn2) { await btn2.click(); await page.waitForTimeout(500); }
  }
  try { await page.waitForSelector('.HouseRules', { timeout: 5000 }); console.log('HouseRules appeared'); } catch(e) { console.error('HouseRules did not appear'); }
  const switches = await page.$$eval('.HouseRules .RuleSwitch', els => els.map(e => ({ id: e.id || '', disabled: e.disabled })));
  console.log('switches', JSON.stringify(switches, null, 2));
  try { await page.screenshot({ path: '/tmp/house-rules.png', fullPage: true }); console.log('screenshot saved to /tmp/house-rules.png'); } catch(e) { console.warn('screenshot failed', e.message); }
  await browser.close();
})();
