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

async function testMasterDashboard() {
  console.log('🎯 Testing Master Dashboard with migrated users...');
  
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  
  try {
    // Login as master trainer
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.waitForLoadState('networkidle');
    
    console.log('🔐 Logging in as master trainer...');
    await page.fill('#user_login', 'joe@measurequick.com');
    await page.fill('#user_pass', 'JoeTrainer2025@');
    await page.click('#wp-submit');
    
    // Should redirect to master dashboard automatically
    await page.waitForTimeout(3000);
    
    const currentUrl = page.url();
    console.log(`📍 Current URL: ${currentUrl}`);
    
    // Make sure we're on master dashboard
    if (!currentUrl.includes('/master-trainer/dashboard/')) {
      await page.goto('https://upskill-staging.measurequick.com/master-trainer/dashboard/');
      await page.waitForLoadState('networkidle');
    }
    
    console.log('📊 Checking master dashboard statistics...');
    
    // Wait for page to load completely
    await page.waitForTimeout(3000);
    
    // Take screenshot
    await page.screenshot({ 
      path: 'test-results/master-dashboard-with-migrated-users.png', 
      fullPage: true 
    });
    
    // Check for trainer statistics
    const trainerStatsVisible = await page.locator('text="Total Trainers"').isVisible();
    console.log(`📋 Trainer statistics visible: ${trainerStatsVisible}`);
    
    // Try to find the trainer count
    const trainerCountElement = await page.locator('text="Total Trainers"').locator('..').locator('p, span, div').first();
    if (await trainerCountElement.isVisible()) {
      const trainerCount = await trainerCountElement.textContent();
      console.log(`👥 Total Trainers shown: ${trainerCount}`);
    }
    
    // Check if we can access the trainer data section
    const trainerDataSection = await page.locator('.trainer-statistics, .trainer-data, [data-tab="trainers"]').isVisible();
    console.log(`📊 Trainer data section visible: ${trainerDataSection}`);
    
    // Look for individual trainer entries
    const trainerEntries = await page.locator('tr, .trainer-entry, .trainer-row').count();
    console.log(`📋 Trainer entries found: ${trainerEntries}`);
    
    console.log('✅ Master dashboard test completed');
    
  } catch (error) {
    console.error('❌ Master dashboard test failed:', error.message);
  } finally {
    await browser.close();
  }
}

testMasterDashboard();