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

(async () => {
  console.log('🔍 Debug master trainer pages with proper authentication...');
  
  const browser = await chromium.launch({
    headless: false,
    slowMo: 1500
  });
  
  const page = await browser.newPage();
  
  try {
    // Step 1: Login properly
    console.log('📋 Step 1: Logging in as test_master...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.waitForTimeout(2000);
    
    await page.fill('#user_login', 'test_master');
    await page.fill('#user_pass', 'TestMaster123!');
    await page.click('#wp-submit');
    await page.waitForTimeout(5000);
    
    // Verify login worked
    const loginSuccess = await page.evaluate(() => {
      return !window.location.href.includes('/training-login/');
    });
    
    console.log('✅ Login Status:', loginSuccess ? 'SUCCESS' : 'FAILED');
    console.log('📍 Current URL after login:', await page.url());
    
    if (!loginSuccess) {
      console.log('❌ Login failed, cannot proceed');
      return;
    }
    
    // Step 2: Test master dashboard directly
    console.log('\n📋 Step 2: Testing master dashboard...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/master-dashboard/');
    await page.waitForTimeout(3000);
    
    const dashboardTest = await page.evaluate(() => ({
      title: document.title,
      url: window.location.href,
      hasContent: document.body.innerText.length > 200,
      bodyText: document.body.innerText.substring(0, 500),
      hasNavigation: !!document.querySelector('.hvac-trainer-menu'),
      navItems: Array.from(document.querySelectorAll('.hvac-trainer-menu a')).length,
      hasHvacWrapper: !!document.querySelector('.hvac-page-wrapper'),
      isLoginPage: document.body.innerText.includes('Sign in to access')
    }));
    
    console.log('📊 Dashboard Test Results:');
    console.log('- Title:', dashboardTest.title);
    console.log('- URL:', dashboardTest.url);
    console.log('- Has Content:', dashboardTest.hasContent);
    console.log('- Is Login Page:', dashboardTest.isLoginPage);
    console.log('- Has Navigation:', dashboardTest.hasNavigation);
    console.log('- Nav Items:', dashboardTest.navItems);
    console.log('- Has HVAC Wrapper:', dashboardTest.hasHvacWrapper);
    console.log('- Body Preview:', dashboardTest.bodyText);
    
    // Step 3: Test trainers page
    console.log('\n📋 Step 3: Testing trainers page...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/trainers/');
    await page.waitForTimeout(3000);
    
    const trainersTest = await page.evaluate(() => ({
      title: document.title,
      url: window.location.href,
      hasContent: document.body.innerText.length > 200,
      isLoginPage: document.body.innerText.includes('Sign in to access'),
      contentPreview: document.body.innerText.substring(0, 300)
    }));
    
    console.log('📊 Trainers Page Test:');
    console.log('- Title:', trainersTest.title);
    console.log('- URL:', trainersTest.url);
    console.log('- Is Login Page:', trainersTest.isLoginPage);
    console.log('- Content Preview:', trainersTest.contentPreview);
    
    // Step 4: Test communication templates
    console.log('\n📋 Step 4: Testing communication templates...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/communication-templates/');
    await page.waitForTimeout(3000);
    
    const templatesTest = await page.evaluate(() => ({
      title: document.title,
      url: window.location.href,
      hasContent: document.body.innerText.length > 200,
      isLoginPage: document.body.innerText.includes('Sign in to access'),
      contentPreview: document.body.innerText.substring(0, 300)
    }));
    
    console.log('📊 Templates Page Test:');
    console.log('- Title:', templatesTest.title);  
    console.log('- URL:', templatesTest.url);
    console.log('- Is Login Page:', templatesTest.isLoginPage);
    console.log('- Content Preview:', templatesTest.contentPreview);
    
    // Keep browser open for inspection
    console.log('\n🔍 Keeping browser open for 30 seconds for inspection...');
    await page.waitForTimeout(30000);
    
  } catch (error) {
    console.error('Error during testing:', error);
  } finally {
    await browser.close();
    console.log('✅ Debug completed');
  }
})();