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

async function testTrainerProfileFix() {
    console.log('🔍 TESTING TRAINER PROFILE TEMPLATE FIX');
    console.log('================================================================================');
    
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext();
    const page = await context.newPage();
    
    try {
        // Login as regular trainer
        console.log('📝 Logging in as regular trainer...');
        await page.goto('https://upskill-staging.measurequick.com/wp-login.php');
        await page.fill('#user_login', 'test_trainer');
        await page.fill('#user_pass', 'TestTrainer123!');
        await page.click('#wp-submit');
        await page.waitForURL('**/trainer/dashboard/**', { timeout: 15000 });
        console.log('✅ Regular trainer login successful');
        
        // Navigate to trainer profile page
        console.log('🔍 Testing trainer profile page...');
        await page.goto('https://upskill-staging.measurequick.com/trainer/profile/');
        await page.waitForLoadState('networkidle');
        
        const profileTestResult = await page.evaluate(() => {
            return {
                url: window.location.href,
                title: document.title,
                hasNavigation: document.querySelector('.hvac-trainer-nav') !== null,
                hasTrainerMenu: document.querySelector('.hvac-trainer-menu') !== null,
                navItemsCount: document.querySelectorAll('.hvac-trainer-menu li').length,
                hasNavButtons: document.querySelectorAll('.hvac-nav-link').length,
                hasBreadcrumbs: document.querySelector('.hvac-breadcrumbs') !== null,
                hasProfileHeader: document.querySelector('.hvac-page-header h1') !== null,
                profileHeaderText: document.querySelector('.hvac-page-header h1')?.textContent || 'none',
                hasProfileSidebar: document.querySelector('.hvac-profile-sidebar') !== null,
                hasProfileMain: document.querySelector('.hvac-profile-main') !== null,
                hasProfileSections: document.querySelectorAll('.hvac-profile-section').length,
                hasStyledButtons: document.querySelectorAll('.hvac-button').length,
                hasPersonalInfo: document.body.innerText.includes('Personal Information'),
                hasCertificationInfo: document.body.innerText.includes('Certification Information'),
                hasProfilePhoto: document.querySelector('.hvac-profile-photo') !== null,
                hasProfileStats: document.querySelector('.hvac-profile-stats') !== null,
                hasInlineStyles: document.querySelector('style') !== null,
                contentPreview: document.body.innerText.slice(0, 500).replace(/\s+/g, ' ').trim()
            };
        });
        
        console.log('📊 Trainer profile test results:', profileTestResult);
        
        if (profileTestResult.hasNavigation && profileTestResult.hasProfileHeader) {
            console.log('🎉 SUCCESS! Trainer profile template is now working!');
            console.log(`   ✅ Navigation menu: ${profileTestResult.hasNavigation} (${profileTestResult.navItemsCount} items)`);  
            console.log(`   ✅ Profile header: ${profileTestResult.profileHeaderText}`);
            console.log(`   ✅ Profile sections: ${profileTestResult.hasProfileSections}`);
            console.log(`   ✅ Breadcrumbs: ${profileTestResult.hasBreadcrumbs}`);
            console.log(`   ✅ Sidebar: ${profileTestResult.hasProfileSidebar}`);
            console.log(`   ✅ Inline styles: ${profileTestResult.hasInlineStyles}`);
            
            await page.screenshot({ path: 'trainer-profile-fixed-success.png', fullPage: true });
            
        } else {
            console.log('❌ Still having issues with trainer profile template');
            console.log(`   Navigation: ${profileTestResult.hasNavigation}`);
            console.log(`   Profile header: ${profileTestResult.hasProfileHeader}`);
            console.log(`   Content preview: ${profileTestResult.contentPreview}`);
            
            await page.screenshot({ path: 'trainer-profile-still-broken.png', fullPage: true });
        }
        
        // Test the Edit Profile button/link
        if (profileTestResult.hasStyledButtons > 0) {
            console.log('🔍 Testing Edit Profile button...');
            const editButton = await page.locator('a[href*="/profile/edit/"]').first();
            if (await editButton.isVisible()) {
                console.log('✅ Edit Profile button is visible and clickable');
            }
        }
        
        console.log('================================================================================');
        console.log('🎯 TRAINER PROFILE TEMPLATE FIX TEST COMPLETE');
        
    } catch (error) {
        console.error('❌ Error during trainer profile test:', error);
        await page.screenshot({ path: 'trainer-profile-test-error.png', fullPage: true });
    } finally {
        await browser.close();
    }
}

testTrainerProfileFix();