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

(async () => {
  console.log('🚀 Starting authenticated browser session...');
  
  const browser = await chromium.launch({
    headless: false,
    slowMo: 1000
  });
  
  const page = await browser.newPage();
  
  try {
    // Step 1: Login first
    console.log('📋 Step 1: Logging in as Test Master...');
    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');
    await page.waitForTimeout(3000);
    
    const loginStatus = await page.evaluate(() => ({
      title: document.title,
      url: window.location.href,
      loggedIn: !window.location.href.includes('training-login')
    }));
    
    console.log('Login Status:', loginStatus);
    
    if (!loginStatus.loggedIn) {
      console.log('❌ Login failed, cannot proceed with tests');
      return;
    }
    
    // Step 2: Test master dashboard 
    console.log('\n📋 Step 2: 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'),
      hasDashboardContent: document.body.innerText.includes('Dashboard') || document.body.innerText.includes('Master'),
      bodyText: document.body.innerText.substring(0, 300),
      url: window.location.href
    }));
    
    console.log('Dashboard Status:', dashboardStatus);
    
    // Step 3: Test trainers page
    console.log('\n📋 Step 3: 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'),
      isLoginRedirect: window.location.href.includes('training-login'),
      bodyPreview: document.body.innerText.substring(0, 300),
      url: window.location.href
    }));
    
    console.log('Trainers Page Status:', trainersStatus);
    
    // Step 4: Test communication templates
    console.log('\n📋 Step 4: 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'),
      isLoginRedirect: window.location.href.includes('training-login'),
      bodyPreview: document.body.innerText.substring(0, 300),
      url: window.location.href
    }));
    
    console.log('Templates Page Status:', templatesStatus);
    
    // Keep browser open for manual inspection
    console.log('\n🔍 Keeping browser open for 15 seconds for manual inspection...');
    await page.waitForTimeout(15000);
    
  } catch (error) {
    console.error('Error during testing:', error);
  } finally {
    await browser.close();
    console.log('✅ Browser session completed');
  }
})();