const { chromium } = require('playwright');

async function testCertificatePages() {
    const browser = await chromium.launch({ headless: false });
    const page = await browser.newContext().then(ctx => ctx.newPage());
    
    console.log('=== Testing Certificate Pages ===\n');
    
    // Login
    console.log('1. Logging in...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.fill('#user_login', 'JoeMedosch@gmail.com');
    await page.fill('#user_pass', 'JoeTrainer2025@');
    await page.locator('input[type="submit"], #wp-submit').first().click();
    await page.waitForLoadState('networkidle');
    await page.waitForTimeout(2000);
    
    // Check if logged in
    const loggedInUrl = page.url();
    console.log('After login URL:', loggedInUrl);
    
    // Test Certificate Reports
    console.log('\n2. Testing Certificate Reports...');
    await page.goto('https://upskill-staging.measurequick.com/trainer/certificate-reports/');
    await page.waitForLoadState('networkidle');
    
    const reportsContent = await page.evaluate(() => {
        return {
            url: window.location.href,
            title: document.title,
            h1: document.querySelector('h1')?.textContent?.trim(),
            h2: document.querySelector('h2')?.textContent?.trim(),
            hasTable: !!document.querySelector('.hvac-certificate-table'),
            tableRows: document.querySelectorAll('.hvac-certificate-table tbody tr').length,
            errorMessage: document.querySelector('.error')?.textContent?.trim()
        };
    });
    
    console.log('Certificate Reports Page:');
    console.log('  URL:', reportsContent.url);
    console.log('  Title:', reportsContent.title);
    console.log('  H1:', reportsContent.h1);
    console.log('  H2:', reportsContent.h2);
    console.log('  Has Table:', reportsContent.hasTable);
    console.log('  Number of Events:', reportsContent.tableRows);
    console.log('  Error:', reportsContent.errorMessage || 'None');
    
    await page.screenshot({ path: 'certificate-reports-test.png', fullPage: true });
    
    // Test Generate Certificates
    console.log('\n3. Testing Generate Certificates...');
    await page.goto('https://upskill-staging.measurequick.com/trainer/generate-certificates/');
    await page.waitForLoadState('networkidle');
    
    const generateContent = await page.evaluate(() => {
        return {
            url: window.location.href,
            title: document.title,
            h1: document.querySelector('h1')?.textContent?.trim(),
            h2: document.querySelector('h2')?.textContent?.trim(),
            hasForm: !!document.querySelector('.hvac-certificate-form'),
            hasEventSelect: !!document.querySelector('#event_select'),
            errorMessage: document.querySelector('.error')?.textContent?.trim()
        };
    });
    
    console.log('Generate Certificates Page:');
    console.log('  URL:', generateContent.url);
    console.log('  Title:', generateContent.title);
    console.log('  H1:', generateContent.h1);
    console.log('  H2:', generateContent.h2);
    console.log('  Has Form:', generateContent.hasForm);
    console.log('  Has Event Select:', generateContent.hasEventSelect);
    console.log('  Error:', generateContent.errorMessage || 'None');
    
    await page.screenshot({ path: 'generate-certificates-test.png', fullPage: true });
    
    await browser.close();
}

testCertificatePages().catch(console.error);