import { test, expect } from '@playwright/test';

test('Critical Error Investigation', async ({ page }) => {
    console.log('=== Investigating Critical Error ===');
    
    // Test main pages
    const pagesToTest = [
        'https://upskill-staging.measurequick.com/',
        'https://upskill-staging.measurequick.com/community-login/',
        'https://upskill-staging.measurequick.com/hvac-dashboard/',
        'https://upskill-staging.measurequick.com/certificate-reports/',
        'https://upskill-staging.measurequick.com/wp-admin/'
    ];
    
    for (const url of pagesToTest) {
        console.log(`\nTesting: ${url}`);
        
        try {
            await page.goto(url, { waitUntil: 'load', timeout: 10000 });
            await page.waitForLoadState('networkidle', { timeout: 5000 });
            
            const title = await page.title();
            console.log(`  Title: ${title}`);
            console.log(`  Final URL: ${page.url()}`);
            
            // Check for critical error
            const hasCriticalError = await page.locator('text=critical error').count() > 0;
            const hasWordPressFatalError = await page.locator('text=There has been a critical error').count() > 0;
            const hasPhpError = await page.locator('text=Fatal error').count() > 0;
            const hasParseError = await page.locator('text=Parse error').count() > 0;
            
            if (hasCriticalError || hasWordPressFatalError || hasPhpError || hasParseError) {
                console.log('  ❌ CRITICAL ERROR DETECTED');
                
                // Get error details
                const errorText = await page.locator('body').textContent();
                const lines = errorText?.split('\n').slice(0, 10) || [];
                console.log('  Error content (first 10 lines):');
                lines.forEach((line, i) => {
                    if (line.trim()) console.log(`    ${i + 1}: ${line.trim()}`);
                });
                
                // Take screenshot
                await page.screenshot({ 
                    path: `critical-error-${url.replace(/[^a-zA-Z0-9]/g, '-')}.png`,
                    fullPage: true 
                });
            } else {
                console.log('  ✅ No critical error detected');
            }
            
            // Check for other indicators
            const bodyText = await page.locator('body').textContent() || '';
            if (bodyText.includes('500')) {
                console.log('  ⚠️  500 error detected');
            }
            if (bodyText.includes('syntax error')) {
                console.log('  ⚠️  Syntax error detected');
            }
            if (bodyText.includes('class not found')) {
                console.log('  ⚠️  Class not found error detected');
            }
            
        } catch (error) {
            console.log(`  ❌ Failed to load: ${error}`);
            await page.screenshot({ 
                path: `failed-load-${url.replace(/[^a-zA-Z0-9]/g, '-')}.png`
            });
        }
    }
    
    console.log('\n=== Investigation Complete ===');
});