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

(async () => {
  console.log('🔍 Testing master dashboard with logged-in session...');
  
  const browser = await chromium.launch({
    headless: false,
    slowMo: 500
  });
  
  const page = await browser.newPage();
  
  try {
    // Login first
    console.log('📋 Logging in as master trainer...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.waitForTimeout(2000);
    
    // Fill login form
    await page.fill('#user_login', 'test_master');
    await page.fill('#user_pass', 'TestMaster123!');
    await page.click('#wp-submit');
    
    // Wait for login to complete
    await page.waitForTimeout(3000);
    
    // Now try to access master dashboard
    console.log('📋 Navigating to master dashboard...');
    const response = await page.goto('https://upskill-staging.measurequick.com/master-trainer/master-dashboard/', {
      waitUntil: 'networkidle'
    });
    
    console.log('- Response status:', response.status());
    console.log('- Final URL:', page.url());
    
    // Check page structure
    const pageInfo = await page.evaluate(() => {
      const bodyClasses = document.body.className;
      const pageId = bodyClasses.match(/page-id-(\d+)/)?.[1];
      
      // Check for content areas
      const mainContent = document.querySelector('#main, .site-main');
      const hvacWrapper = document.querySelector('.hvac-page-wrapper');
      const dashboardContent = document.querySelector('.hvac-dashboard-stats, .hvac-stat-card');
      
      // Get any visible text content
      const visibleText = document.body.innerText.substring(0, 500);
      
      return {
        pageId: pageId || 'none',
        templateClass: bodyClasses.match(/page-template-[\w-]+/)?.[0] || 'none',
        hasMainContent: mainContent !== null,
        hasHvacWrapper: hvacWrapper !== null,
        hasDashboardContent: dashboardContent !== null,
        mainContentHTML: mainContent ? mainContent.innerHTML.substring(0, 200) : 'none',
        visibleTextSnippet: visibleText
      };
    });
    
    console.log('\n📊 Page Analysis:');
    console.log('- WordPress Page ID:', pageInfo.pageId);
    console.log('- Template Class:', pageInfo.templateClass);
    console.log('- Has Main Content:', pageInfo.hasMainContent);
    console.log('- Has HVAC Wrapper:', pageInfo.hasHvacWrapper);
    console.log('- Has Dashboard Content:', pageInfo.hasDashboardContent);
    console.log('\n- Main Content HTML (first 200 chars):');
    console.log(pageInfo.mainContentHTML);
    console.log('\n- Visible Text (first 500 chars):');
    console.log(pageInfo.visibleTextSnippet);
    
    // Check HTML source
    const htmlContent = await page.content();
    console.log('\n📊 HTML Source Analysis:');
    console.log('- Contains "Master Dashboard":', htmlContent.includes('Master Dashboard'));
    console.log('- Contains "System Overview":', htmlContent.includes('System Overview'));
    console.log('- Contains debug comment:', htmlContent.includes('DEBUG: template-hvac-master-dashboard.php loaded'));
    console.log('- Page length:', htmlContent.length, 'characters');
    
    // Take screenshot
    await page.screenshot({ 
      path: '/tmp/playwright-mcp-output/master-logged-in.png',
      fullPage: false 
    });
    console.log('\n📸 Screenshot saved');
    
    console.log('\n🔍 Keeping browser open for 15 seconds...');
    await page.waitForTimeout(15000);
    
  } catch (error) {
    console.error('Error during testing:', error);
  } finally {
    await browser.close();
    console.log('✅ Test completed');
  }
})();