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

(async () => {
  console.log('🔍 Checking if master dashboard page exists in WordPress...');
  
  const browser = await chromium.launch({
    headless: false,
    slowMo: 500
  });
  
  const page = await browser.newPage();
  
  try {
    // First check what happens with a simple direct URL access
    console.log('📋 Testing direct URL access...');
    const response = await page.goto('https://upskill-staging.measurequick.com/master-trainer/master-dashboard/', {
      waitUntil: 'networkidle'
    });
    
    console.log('- Response status:', response.status());
    console.log('- Response URL:', response.url());
    
    // Check page structure
    const pageInfo = await page.evaluate(() => {
      const bodyClasses = document.body.className;
      const pageId = bodyClasses.match(/page-id-(\d+)/)?.[1];
      
      // Check for WordPress's own 404 indicators
      const is404 = bodyClasses.includes('error404');
      const hasContent = document.querySelector('.entry-content, .site-main, #main, .hvac-page-wrapper');
      
      // Check what template WordPress thinks it's using
      const templateClass = bodyClasses.match(/page-template-[\w-]+/)?.[0];
      
      // Check if there's a title element
      const title = document.querySelector('h1, .entry-title');
      
      return {
        pageId: pageId || 'none',
        is404: is404,
        hasContent: hasContent !== null,
        contentSelector: hasContent ? hasContent.tagName + (hasContent.id ? '#' + hasContent.id : '') + (hasContent.className ? '.' + hasContent.className.split(' ')[0] : '') : 'none',
        templateClass: templateClass || 'none',
        pageTitle: title ? title.textContent.trim() : 'none',
        bodyClassList: bodyClasses
      };
    });
    
    console.log('\n📊 Page Analysis:');
    console.log('- WordPress Page ID:', pageInfo.pageId);
    console.log('- Is 404 Page:', pageInfo.is404);
    console.log('- Has Content Area:', pageInfo.hasContent);
    console.log('- Content Selector:', pageInfo.contentSelector);
    console.log('- Template Class:', pageInfo.templateClass);
    console.log('- Page Title:', pageInfo.pageTitle);
    
    // Try alternate URL formats
    console.log('\n📋 Testing alternate URL formats...');
    
    // Try without trailing slash
    const response2 = await page.goto('https://upskill-staging.measurequick.com/master-trainer/master-dashboard', {
      waitUntil: 'networkidle'
    });
    console.log('- Without trailing slash - Status:', response2.status(), 'URL:', response2.url());
    
    // Try the old dashboard URL
    const response3 = await page.goto('https://upskill-staging.measurequick.com/master-trainer/dashboard/', {
      waitUntil: 'networkidle'
    });
    console.log('- Old dashboard URL - Status:', response3.status(), 'URL:', response3.url());
    
    // Check what's in the HTML source
    const htmlContent = await page.content();
    console.log('\n📊 HTML Content Analysis:');
    console.log('- Contains "Master Dashboard":', htmlContent.includes('Master Dashboard'));
    console.log('- Contains "System Overview":', htmlContent.includes('System Overview'));
    console.log('- Contains template debug comment:', htmlContent.includes('DEBUG: template-hvac-master-dashboard.php loaded'));
    console.log('- Contains hvac-page-wrapper:', htmlContent.includes('hvac-page-wrapper'));
    
    // Look for any PHP errors in the source
    const phpErrors = htmlContent.match(/Fatal error:|Warning:|Notice:|Parse error:/gi);
    if (phpErrors) {
      console.log('\n⚠️  PHP Errors found:', phpErrors);
    }
    
    // Take screenshot
    await page.screenshot({ 
      path: '/tmp/playwright-mcp-output/wp-page-check.png',
      fullPage: false 
    });
    console.log('\n📸 Screenshot saved to /tmp/playwright-mcp-output/wp-page-check.png');
    
    console.log('\n🔍 Keeping browser open for 10 seconds...');
    await page.waitForTimeout(10000);
    
  } catch (error) {
    console.error('Error during testing:', error);
  } finally {
    await browser.close();
    console.log('✅ Test completed');
  }
})();