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

(async () => {
    const browser = await chromium.launch();
    const context = await browser.newContext();
    const page = await context.newPage();
    
    // Create screenshots directory
    const screenshotDir = path.join(__dirname, '../screenshots/template-verification');
    if (!fs.existsSync(screenshotDir)) {
        fs.mkdirSync(screenshotDir, { recursive: true });
    }
    
    const baseUrl = 'https://upskill-staging.measurequick.com';
    
    // Test pages to verify
    const testPages = [
        { name: 'trainer-dashboard', url: '/trainer/dashboard/', expectedContent: 'Welcome' },
        { name: 'certificate-reports', url: '/trainer/certificate-reports/', expectedContent: 'Certificate Reports' },
        { name: 'generate-certificates', url: '/trainer/generate-certificates/', expectedContent: 'Generate Certificates' },
        { name: 'master-dashboard', url: '/master-trainer/dashboard/', expectedContent: 'Master Dashboard' },
        { name: 'trainer-registration', url: '/trainer/registration/', expectedContent: 'Trainer Registration' }
    ];
    
    console.log('Starting page template verification...');
    
    for (const testPage of testPages) {
        console.log(`\nTesting ${testPage.name}...`);
        
        try {
            await page.goto(baseUrl + testPage.url, { waitUntil: 'networkidle' });
            
            // Check if we're redirected to login
            const currentUrl = page.url();
            if (currentUrl.includes('training-login')) {
                console.log(`✓ ${testPage.name} - Redirected to login (authentication required)`);
            } else {
                // Check for expected content
                const bodyText = await page.textContent('body');
                if (bodyText.includes(testPage.expectedContent)) {
                    console.log(`✓ ${testPage.name} - Content found: "${testPage.expectedContent}"`);
                } else {
                    console.log(`✗ ${testPage.name} - Expected content not found`);
                }
                
                // Check if page has sidebar (indicates template issue)
                const hasSidebar = await page.locator('.widget-area, .sidebar, #secondary').count();
                if (hasSidebar > 0) {
                    console.log(`⚠️  ${testPage.name} - Has sidebar (possible template issue)`);
                } else {
                    console.log(`✓ ${testPage.name} - No sidebar (template working)`);
                }
            }
            
            // Take screenshot
            await page.screenshot({ 
                path: path.join(screenshotDir, `${testPage.name}.png`),
                fullPage: true
            });
            
        } catch (error) {
            console.log(`✗ ${testPage.name} - Error: ${error.message}`);
        }
    }
    
    console.log('\nVerification complete. Screenshots saved to:', screenshotDir);
    
    await browser.close();
})();