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

async function testJoeAccounts() {
  console.log('🔐 Testing Joe account access to master trainer functionality...');
  
  const browser = await chromium.launch();
  
  const joeAccounts = [
    {
      username: 'joe@measurequick.com',
      password: 'JoeTrainer2025@',
      name: 'Joe MeasureQuick'
    },
    {
      username: 'joe@upskillhvac.com', 
      password: 'JoeTrainer2025@',
      name: 'Joe UpskillHVAC'
    }
  ];
  
  for (const account of joeAccounts) {
    console.log(`\n🧪 Testing ${account.name} (${account.username})...`);
    
    const page = await browser.newPage();
    
    try {
      // Test login
      await page.goto('https://upskill-staging.measurequick.com/training-login/');
      await page.waitForLoadState('networkidle');
      
      await page.fill('#user_login', account.username);
      await page.fill('#user_pass', account.password);
      await page.click('#wp-submit');
      
      await page.waitForURL('**/trainer/dashboard/**', { timeout: 10000 });
      console.log('✅ Login successful');
      
      // Test regular trainer dashboard access
      const trainerDashboard = page.url().includes('/trainer/dashboard/');
      console.log(`📊 Trainer dashboard access: ${trainerDashboard}`);
      
      // Test master trainer dashboard access
      await page.goto('https://upskill-staging.measurequick.com/master-trainer/dashboard/');
      await page.waitForLoadState('networkidle');
      
      // Check if we're still on the master dashboard (not redirected due to permissions)
      const masterDashboard = page.url().includes('/master-trainer/dashboard/');
      console.log(`🎯 Master trainer dashboard access: ${masterDashboard}`);
      
      // Take screenshot for verification
      await page.screenshot({ 
        path: `test-results/joe-${account.username.replace('@', '-at-')}-master-dashboard.png`, 
        fullPage: true 
      });
      
      if (masterDashboard) {
        console.log(`✅ ${account.name} has master trainer access!`);
      } else {
        console.log(`❌ ${account.name} lacks master trainer access`);
      }
      
    } catch (error) {
      console.error(`❌ Test failed for ${account.name}:`, error.message);
    } finally {
      await page.close();
    }
  }
  
  await browser.close();
  console.log('\n✅ Joe account testing completed!');
}

testJoeAccounts();