const puppeteer = require('puppeteer');
const AuthHelper = require('./auth_helper');

async function captureMetricsSubpages() {
    const browser = await puppeteer.launch({
        headless: false,
        args: ['--no-sandbox', '--disable-setuid-sandbox']
    });

    try {
        const page = await browser.newPage();
        await page.setViewport({ width: 1280, height: 720 });

        const auth = new AuthHelper(page);
        await auth.ensureAuthenticated();

        // List of subpages to capture
        const subpages = [
            { url: 'http://127.0.0.1:5000/metrics/context-overflow', name: 'context-overflow' },
            { url: 'http://127.0.0.1:5000/metrics/links', name: 'links' },
            { url: 'http://127.0.0.1:5000/metrics/costs', name: 'costs' },
            { url: 'http://127.0.0.1:5000/metrics/star-reviews', name: 'star-reviews' }
        ];

        for (const subpage of subpages) {
            console.log(`\nNavigating to ${subpage.name}...`);

            await page.goto(subpage.url, {
                waitUntil: 'networkidle2',
                timeout: 30000
            });

            // Wait for content to load
            await new Promise(resolve => setTimeout(resolve, 3000));

            // Take screenshot
            const screenshotPath = `./metrics-${subpage.name}.png`;
            await page.screenshot({
                path: screenshotPath,
                fullPage: true
            });

            console.log(`✅ Screenshot saved: ${screenshotPath}`);

            // Get page title and basic info
            const pageInfo = await page.evaluate(() => {
                return {
                    title: document.title,
                    hasContent: document.body.innerText.length > 100,
                    url: window.location.href,
                    hasCharts: !!document.querySelector('canvas'),
                    hasTables: !!document.querySelector('table'),
                    mainHeading: document.querySelector('h1, h2')?.innerText || 'No heading found'
                };
            });

            console.log(`Page info for ${subpage.name}:`, JSON.stringify(pageInfo, null, 2));
        }

        console.log('\n✅ All screenshots captured successfully!');

    } catch (error) {
        console.error('Error:', error);
    } finally {
        await browser.close();
    }
}

captureMetricsSubpages().catch(console.error);
