import { chromium } from '@playwright/test'
import { writeFileSync } from 'node:fs'
const OUT = 'D:/Temp/claude/D--JobRoomz-KArtSell-Aegis/da832daa-83e6-4660-8d4b-a698136f10e3/scratchpad/pw-capture'
const BASE = 'http://127.0.0.1:5173'
const pages = [
  ['31-sell-decision', '/research/sell-decision'],
  ['32-data-quality', '/ops/data-quality'],
  ['33-rebalance', '/portfolio/rebalance'],
  ['34-market-ingestion', '/ops/market-data-ingestion'],
  ['35-ingestion-status', '/ops/market-data-history'],
  ['36-risk-dashboard', '/portfolio/risk'],
]

const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } })
const consoleLog = []
const pageErrors = []
page.on('console', msg => { if (msg.type() === 'error' || msg.type() === 'warning') consoleLog.push(`[${msg.type()}] ${msg.text()}`) })
page.on('pageerror', err => pageErrors.push(String(err)))

for (const [name, path] of pages) {
  consoleLog.push(`--- ${name} ${path} ---`)
  await page.goto(BASE + path, { waitUntil: 'networkidle' }).catch(e => pageErrors.push(`goto ${path}: ${e}`))
  await page.waitForTimeout(400)
  await page.screenshot({ path: `${OUT}/${name}.png`, fullPage: true })
}

writeFileSync(`${OUT}/retrofit-console.log`, consoleLog.join('\n'))
writeFileSync(`${OUT}/retrofit-page-errors.log`, pageErrors.join('\n'))
await browser.close()
console.log('DONE', 'console:', consoleLog.length, 'errors:', pageErrors.length)
