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

(async () => {
  console.log('🚀 Starting headed browser session to verify master trainer pages...');
  
  const browser = await chromium.launch({
    headless: false,
    slowMo: 1000
  });
  
  const page = await browser.newPage();
  
  try {
    // Test 1: Master Dashboard
    console.log('📋 Testing master-trainer/master-dashboard/...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/master-dashboard/');
    await page.waitForTimeout(3000);
    
    const dashboardStatus = await page.evaluate(() => ({
      title: document.title,
      hasContent: !!document.getElementById('content'),
      contentLength: document.getElementById('content')?.innerHTML.length || 0,
      hasNavigation: !!document.querySelector('.hvac-trainer-menu'),
      bodyText: document.body.innerText.substring(0, 200),
      is404: document.body.innerText.includes('404') || document.body.innerText.includes('Page not found')
    }));
    
    console.log('Dashboard Status:', dashboardStatus);
    
    // Test 2: Trainers Page
    console.log('\n📋 Testing master-trainer/trainers/...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/trainers/');
    await page.waitForTimeout(3000);
    
    const trainersStatus = await page.evaluate(() => ({
      title: document.title,
      is404: document.body.innerText.includes('404') || document.body.innerText.includes('Page not found'),
      bodyPreview: document.body.innerText.substring(0, 200),
      url: window.location.href
    }));
    
    console.log('Trainers Page Status:', trainersStatus);
    
    // Test 3: Communication Templates
    console.log('\n📋 Testing master-trainer/communication-templates/...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/communication-templates/');
    await page.waitForTimeout(3000);
    
    const templatesStatus = await page.evaluate(() => ({
      title: document.title,
      is404: document.body.innerText.includes('404') || document.body.innerText.includes('Page not found'),
      bodyPreview: document.body.innerText.substring(0, 200),
      url: window.location.href
    }));
    
    console.log('Templates Page Status:', templatesStatus);
    
    // Keep browser open for 10 seconds for manual inspection
    console.log('\n🔍 Keeping browser open for 10 seconds for manual inspection...');
    await page.waitForTimeout(10000);
    
  } catch (error) {
    console.error('Error during testing:', error);
  } finally {
    await browser.close();
    console.log('✅ Browser session completed');
  }
})();