import { runAll, summarize, allChecks } from './framework.js';

// Registering a phase's checks is a side effect of importing it.
import './phase0.js';
import './phase1.js';
import './phase2.js';
import './phase3.js';
import './phase4.js';
import './phase5.js';
import './phase6.js';

const out = document.getElementById('results');
const summaryEl = document.getElementById('summary');

function row(result) {
    const el = document.createElement('div');
    el.className = 'row ' + (result.pass ? 'pass' : result.manual ? 'manual' : 'fail');
    el.innerHTML = `
        <span class="badge">${result.pass ? 'PASS' : 'FAIL'}</span>
        <span class="phase">P${result.phase}</span>
        <span class="name">${result.name}</span>
        <span class="ms">${result.ms}ms</span>
        <div class="detail">${result.detail || ''}</div>`;
    out.appendChild(el);
}

async function main() {
    const params = new URLSearchParams(location.search);
    const phaseArg = params.get('phase');
    const phases = phaseArg ? phaseArg.split(',').map(Number) : null;
    const skipSlow = params.get('slow') !== '1';

    summaryEl.textContent = `running ${allChecks().length} checks…`;

    const results = await runAll({ phases, skipSlow, onResult: row });
    const s = summarize(results);

    summaryEl.textContent =
        `${s.passed}/${s.total} passed · ${s.failed} failed` + (skipSlow ? ' · slow checks skipped (?slow=1 to include)' : '');
    summaryEl.className = s.ok ? 'ok' : 'bad';

    // Read by the browser automation that drives these gates.
    window.__CHECKS__ = { results, summary: s };
    window.__CHECKS_DONE__ = true;
    console.log('[checks]', JSON.stringify(s, null, 2));
}

main();
