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

(async () => {
    console.log('🚨 INCIDENT RESPONSE: Testing Master Trainer Pages');
    console.log('=====================================');
    
    const browser = await chromium.launch({ 
        headless: false,
        args: ['--no-sandbox']
    });
    
    const context = await browser.newContext({
        viewport: { width: 1280, height: 720 }
    });
    
    const page = await context.newPage();
    
    // First login as master trainer
    console.log('\n1️⃣ Logging in as Master Trainer...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.waitForTimeout(2000);
    
    // Login
    await page.fill('input[name="log"]', 'JoeMedosch@gmail.com');
    await page.fill('input[name="pwd"]', 'JoeTrainer2025@');
    await page.click('input[type="submit"][value="Log In"]');
    await page.waitForTimeout(3000);
    
    // Test each problematic page
    const pages = [
        { url: '/master-trainer/announcements/', name: 'Announcements' },
        { url: '/master-trainer/pending-approvals/', name: 'Pending Approvals' },
        { url: '/master-trainer/trainers/', name: 'Trainers Overview' },
        { url: '/master-trainer/events/', name: 'Events Overview' }
    ];
    
    for (const testPage of pages) {
        console.log(`\n📄 Testing ${testPage.name}...`);
        console.log(`   URL: ${testPage.url}`);
        
        await page.goto(`https://upskill-staging.measurequick.com${testPage.url}`);
        await page.waitForTimeout(3000);
        
        // Check for content
        const title = await page.title();
        console.log(`   Title: ${title}`);
        
        // Check for any error messages
        const bodyText = await page.evaluate(() => document.body.innerText);
        
        // Look for specific indicators
        const hasNavigation = await page.locator('.hvac-trainer-menu').count() > 0;
        const hasBreadcrumbs = await page.locator('.hvac-breadcrumbs').count() > 0;
        const hasContent = bodyText.length > 500; // More than just header/footer
        
        console.log(`   ✓ Navigation present: ${hasNavigation}`);
        console.log(`   ✓ Breadcrumbs present: ${hasBreadcrumbs}`);
        console.log(`   ✓ Content length: ${bodyText.length} chars`);
        
        // Check for shortcode output
        const hasShortcodeContent = bodyText.includes('Overview') || 
                                   bodyText.includes('Announcements') || 
                                   bodyText.includes('Pending') ||
                                   bodyText.includes('Events') ||
                                   bodyText.includes('Trainers');
        
        console.log(`   ✓ Has relevant content: ${hasShortcodeContent}`);
        
        // Look for error indicators
        if (bodyText.includes('Page not found') || bodyText.includes('404')) {
            console.log(`   ❌ ERROR: 404 Page Not Found`);
        }
        
        if (bodyText.length < 500) {
            console.log(`   ⚠️ WARNING: Page appears to have no content (only ${bodyText.length} chars)`);
            console.log(`   First 200 chars: ${bodyText.substring(0, 200)}`);
        }
        
        // Take screenshot for visual inspection
        await page.screenshot({ 
            path: `master-${testPage.name.toLowerCase().replace(/ /g, '-')}-incident.png`,
            fullPage: true
        });
        console.log(`   📸 Screenshot saved: master-${testPage.name.toLowerCase().replace(/ /g, '-')}-incident.png`);
    }
    
    console.log('\n✅ Test complete. Check screenshots for visual verification.');
    
    await browser.close();
})();
