// Shared helpers for Playwright tests

const BASE_URL = process.env.SX_TEST_URL || 'http://localhost:8013';

/**
 * Wait for the SX runtime to finish hydration.
 * boot-init sets data-sx-ready="true" on <html> after all islands are hydrated.
 */
async function waitForSxReady(page, timeout = 15000) {
  await page.waitForSelector('html[data-sx-ready]', { timeout });
}

/**
 * Navigate to an SX page and wait for hydration to complete.
 * Replaces the old pattern of networkidle + arbitrary sleep.
 */
async function loadPage(page, path, timeout = 15000) {
  await page.goto(BASE_URL + '/sx/' + path, { waitUntil: 'domcontentloaded', timeout });
  await waitForSxReady(page);
}

/**
 * Track console errors and uncaught exceptions on a page.
 * Call before navigation. Use errors() to get filtered error list.
 */
function trackErrors(page) {
  const raw = [];
  page.on('pageerror', err => raw.push(err.message));
  page.on('console', msg => {
    if (msg.type() === 'error') raw.push(msg.text());
  });
  return {
    /** Return errors, filtering transient network noise. */
    errors() {
      return raw.filter(e =>
        !e.includes('Failed to fetch') &&
        !e.includes('net::ERR') &&
        !e.includes(' 404 ') &&
        !e.includes('Failed to load resource') &&
        !e.includes('Parse_error')  // WASM parser edge case on empty OOB fragments
      );
    }
  };
}

/**
 * Universal smoke checks for any SX page.
 * Runs 10 assertions that every page should pass.
 * Returns { pass: boolean, failures: string[] }.
 */
async function universalSmoke(page) {
  const failures = [];
  const warnings = [];

  // 1. Not blank — #sx-content has substantial text
  const contentLen = await page.evaluate(() => {
    const el = document.querySelector('#sx-content') || document.body;
    return el.textContent.length;
  });
  if (contentLen < 50) failures.push(`blank page (${contentLen} chars)`);

  // 2. Has heading (soft — some index/demo pages legitimately lack headings)
  const headingCount = await page.locator('h1, h2, h3, h4').count();
  if (headingCount === 0) warnings.push('no heading (h1-h4)');

  // 3. Title set
  const title = await page.title();
  if (!title || title === 'about:blank') failures.push(`title not set: "${title}"`);

  // 4. Layout intact — #sx-nav exists
  const navExists = await page.locator('#sx-nav').count();
  if (navExists === 0) failures.push('no #sx-nav');

  // 5. No duplicate structural IDs
  const dupes = await page.evaluate(() => {
    const ids = ['sx-nav', 'sx-content', 'main-panel'];
    return ids.filter(id => document.querySelectorAll('#' + id).length > 1);
  });
  if (dupes.length > 0) failures.push(`duplicate IDs: ${dupes.join(', ')}`);

  // 6. No broken hrefs — no [object Object] in links
  const brokenLinks = await page.evaluate(() => {
    return [...document.querySelectorAll('a[href]')]
      .filter(a => a.href.includes('[object Object]'))
      .length;
  });
  if (brokenLinks > 0) failures.push(`${brokenLinks} broken [object Object] links`);

  // 7. CSSX present
  const cssxCount = await page.locator('style[id="sx-css"], style[data-sx-css]').count();
  if (cssxCount === 0) failures.push('no CSSX style tag');

  // 8. No hard SX leaks — raw dicts, raw SX elements, [object Object]
  //    (Unresolved components like ~tw and CSSX :tokens are expected in SSR-only mode)
  const leaks = await page.evaluate(() => {
    const skip = new Set();
    document.querySelectorAll('code, pre, script, style, [data-sx-source]').forEach(el => {
      el.querySelectorAll('*').forEach(d => skip.add(d));
      skip.add(el);
    });
    const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
      acceptNode: (node) => {
        let p = node.parentElement;
        while (p) {
          if (skip.has(p)) return NodeFilter.FILTER_REJECT;
          p = p.parentElement;
        }
        return NodeFilter.FILTER_ACCEPT;
      }
    });
    let text = '';
    let n;
    while (n = walker.nextNode()) text += n.textContent;
    const found = [];
    if (/\{:(?:type|tag|expr|spreads|attrs)\s/.test(text)) found.push('raw-dict');
    if (/\((?:div|span|h[1-6]|p|a|button)\s+:(?:class|id|style)/.test(text)) found.push('raw-sx-element');
    if (/\[object Object\]/.test(text)) found.push('object-Object');
    return found;
  });
  if (leaks.length > 0) failures.push(`SX leaks: ${leaks.join(', ')}`);

  // 9. No console errors (checked by caller via trackErrors)
  // — intentionally not checked here; caller should use trackErrors()

  // 10. Hydration (optional — pages served via route interception may not hydrate)
  // — checked by caller if applicable

  return { pass: failures.length === 0, failures, warnings };
}

module.exports = { BASE_URL, waitForSxReady, loadPage, trackErrors, universalSmoke };
