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

async function debugProfileTemplate() {
    console.log('🔍 DEBUGGING PROFILE TEMPLATE SYSTEM');
    console.log('================================================================================');
    
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext();
    const page = await context.newPage();
    
    try {
        // Login as test trainer
        console.log('📝 Logging in as test 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/**');
        console.log('✅ Login successful');
        
        // Go to profile page and check what's loaded
        console.log('🔍 Checking profile page source...');
        await page.goto('https://upskill-staging.measurequick.com/trainer/profile/');
        await page.waitForLoadState('networkidle');
        
        // Check page body classes
        const bodyClasses = await page.getAttribute('body', 'class');
        console.log(`📋 Body classes: ${bodyClasses}`);
        
        // Check for template indicators
        const templateIndicators = await page.evaluate(() => {
            return {
                hasHvacWrapper: document.querySelector('.hvac-page-wrapper') !== null,
                hasProfileContainer: document.querySelector('.hvac-trainer-profile-view') !== null,
                hasContainer: document.querySelector('.container') !== null,
                pageContent: document.querySelector('main, .content, .entry-content') ? 
                    document.querySelector('main, .content, .entry-content').innerText.slice(0, 200) : 'No main content found',
                shortcodes: document.body.innerHTML.includes('[hvac_') || document.body.innerHTML.includes('[trainer_'),
                templateConstant: document.body.innerHTML.includes('HVAC_IN_PAGE_TEMPLATE')
            };
        });
        
        console.log('🔍 Template analysis:', templateIndicators);
        
        // Check what the page is actually loading
        const pageSource = await page.content();
        const isUsingShortcode = pageSource.includes('Trainer profile view - Updated template will handle display');
        console.log(`📄 Using shortcode fallback: ${isUsingShortcode}`);
        
        // Check for WordPress template hierarchy
        const wpTemplateInfo = await page.evaluate(() => {
            const bodyClasses = document.body.className;
            return {
                isPageTemplate: bodyClasses.includes('page-template'),
                templateName: bodyClasses.match(/page-template-([^\s]+)/)?.[1] || 'none',
                isCustomTemplate: bodyClasses.includes('page-trainer-profile')
            };
        });
        
        console.log('🎯 WordPress template info:', wpTemplateInfo);
        
        // Take detailed screenshot
        await page.screenshot({ path: 'profile-template-debug.png', fullPage: true });
        
        // Check edit page too
        console.log('🔍 Checking edit page source...');
        await page.goto('https://upskill-staging.measurequick.com/trainer/profile/edit/');
        await page.waitForLoadState('networkidle');
        
        const editSource = await page.content();
        const editUsingShortcode = editSource.includes('Trainer profile edit - Updated template will handle editing');
        console.log(`📄 Edit using shortcode fallback: ${editUsingShortcode}`);
        
        await page.screenshot({ path: 'profile-edit-template-debug.png', fullPage: true });
        
        console.log('================================================================================');
        console.log('🎯 TEMPLATE DEBUG COMPLETE');
        
    } catch (error) {
        console.error('❌ Error during template debug:', error);
        await page.screenshot({ path: 'template-debug-error.png', fullPage: true });
    } finally {
        await browser.close();
    }
}

debugProfileTemplate();