#!/usr/bin/env node

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

(async () => {
    console.log('🔍 Testing Master Trainer Content Display...');
    
    const browser = await chromium.launch({ 
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox']
    });
    
    const page = await browser.newPage();
    
    try {
        // Login as master trainer
        console.log('📝 Logging in as master trainer...');
        await page.goto('https://upskill-staging.measurequick.com/training-login/');
        await page.waitForSelector('input[name="log"]', { timeout: 10000 });
        await page.fill('input[name="log"]', 'JoeMedosch@gmail.com');
        await page.fill('input[name="pwd"]', 'JoeTrainer2025@');
        await page.click('input[name="wp-submit"]');
        await page.waitForLoadState('networkidle');
        
        // Test All Trainers page
        console.log('🧑‍🏫 Testing All Trainers page...');
        await page.goto('https://upskill-staging.measurequick.com/master-trainer/all-trainers/');
        await page.waitForSelector('.hvac-master-trainers-content', { timeout: 5000 });
        
        const trainersContent = await page.textContent('.hvac-master-trainers-content');
        console.log('All Trainers Content Preview:', trainersContent.substring(0, 200) + '...');
        
        const hasTrainersList = await page.locator('.hvac-trainers-grid, .hvac-trainers-list, table').count() > 0;
        console.log(hasTrainersList ? '✅ All Trainers page has content' : '❌ All Trainers page missing content');
        
        // Test Events Overview page
        console.log('📅 Testing Events Overview page...');
        await page.goto('https://upskill-staging.measurequick.com/master-trainer/events-overview/');
        await page.waitForSelector('.hvac-master-events-content', { timeout: 5000 });
        
        const eventsContent = await page.textContent('.hvac-master-events-content');
        console.log('Events Overview Content Preview:', eventsContent.substring(0, 200) + '...');
        
        const hasEventsKPIs = await page.locator('.hvac-kpi-card, .hvac-event-kpi, .kpi-summary').count() > 0;
        console.log(hasEventsKPIs ? '✅ Events Overview page has content' : '❌ Events Overview page missing content');
        
        // Test Pending Approvals page
        console.log('⏳ Testing Pending Approvals page...');
        await page.goto('https://upskill-staging.measurequick.com/master-trainer/pending-approvals/');
        await page.waitForSelector('.hvac-master-pending-approvals-page', { timeout: 5000 });
        
        const approvalsContent = await page.textContent('.container');
        console.log('Pending Approvals Content Preview:', approvalsContent.substring(0, 200) + '...');
        
        const hasPendingApprovals = await page.locator('.hvac-approval-item, .pending-approval, .approval-queue').count() > 0;
        const hasEmptyState = approvalsContent.includes('No pending approvals') || approvalsContent.includes('no pending');
        console.log((hasPendingApprovals || hasEmptyState) ? '✅ Pending Approvals page has content' : '❌ Pending Approvals page missing content');
        
        // Test Announcements page
        console.log('📢 Testing Announcements page...');
        await page.goto('https://upskill-staging.measurequick.com/master-trainer/announcements/');
        await page.waitForSelector('.hvac-master-announcements-page', { timeout: 5000 });
        
        const announcementsContent = await page.textContent('.announcements-content');
        console.log('Announcements Content Preview:', announcementsContent.substring(0, 200) + '...');
        
        const hasAnnouncements = await page.locator('.hvac-announcement, .announcement-item, .timeline-item').count() > 0;
        const hasAnnouncementSystem = announcementsContent.includes('announcement') || announcementsContent.includes('timeline');
        console.log((hasAnnouncements || hasAnnouncementSystem) ? '✅ Announcements page has content' : '❌ Announcements page missing content');
        
        console.log('\n🎉 Master Trainer Content Test Complete!');
        
    } catch (error) {
        console.error('❌ Test failed:', error.message);
    } finally {
        await browser.close();
    }
})();