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

(async () => {
  console.log('🚀 Final verification of master trainer fixes...');
  
  const browser = await chromium.launch({
    headless: false,
    slowMo: 1500
  });
  
  const page = await browser.newPage();
  
  try {
    // Step 1: Login 
    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(3000);
    
    // Step 2: Test Dashboard (baseline)
    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,
      hasNavigation: !!document.querySelector('.hvac-trainer-menu'),
      navigationItems: Array.from(document.querySelectorAll('.hvac-trainer-menu a')).length,
      url: window.location.href
    }));
    
    console.log('✅ Dashboard Status:', dashboardTest);
    
    // Step 3: Test Trainers Page (previously 404)
    console.log('\n📋 Step 3: Testing master-trainer/trainers/ (was 404)...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/trainers/');
    await page.waitForTimeout(3000);
    
    const trainersTest = await page.evaluate(() => ({
      title: document.title,
      is404: document.body.innerText.includes('404') || document.body.innerText.includes('Page not found') || document.title.includes('Page not found'),
      hasContent: document.body.innerText.length > 100,
      bodyPreview: document.body.innerText.substring(0, 200),
      url: window.location.href
    }));
    
    console.log('📊 Trainers Page Test:', trainersTest);
    console.log(trainersTest.is404 ? '❌ STILL 404' : '✅ PAGE LOADS');
    
    // Step 4: Test Events Page (newly created)
    console.log('\n📋 Step 4: Testing master-trainer/events/ (newly created)...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/events/');
    await page.waitForTimeout(3000);
    
    const eventsTest = await page.evaluate(() => ({
      title: document.title,
      is404: document.body.innerText.includes('404') || document.body.innerText.includes('Page not found') || document.title.includes('Page not found'),
      hasContent: document.body.innerText.length > 100,
      bodyPreview: document.body.innerText.substring(0, 200),
      url: window.location.href
    }));
    
    console.log('📊 Events Page Test:', eventsTest);
    console.log(eventsTest.is404 ? '❌ 404 ERROR' : '✅ PAGE LOADS');
    
    // Step 5: Test Communication Templates (should work)
    console.log('\n📋 Step 5: Testing master-trainer/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,
      isCorrectURL: window.location.href.includes('/master-trainer/communication-templates/'),
      hasContent: document.body.innerText.length > 100,
      bodyPreview: document.body.innerText.substring(0, 200),
      url: window.location.href
    }));
    
    console.log('📊 Templates Page Test:', templatesTest);
    console.log(templatesTest.isCorrectURL ? '✅ CORRECT URL' : '❌ URL REDIRECT');
    
    // Summary
    console.log('\n🎯 FINAL SUMMARY:');
    console.log('Dashboard:', dashboardTest.hasNavigation ? '✅ Working' : '❌ Issues');
    console.log('Trainers:', !trainersTest.is404 ? '✅ Fixed (was 404)' : '❌ Still 404');
    console.log('Events:', !eventsTest.is404 ? '✅ Created successfully' : '❌ Creation failed');
    console.log('Templates:', templatesTest.isCorrectURL ? '✅ Working' : '❌ URL issues');
    
    // Keep browser open for inspection
    console.log('\n🔍 Keeping browser open for 20 seconds for inspection...');
    await page.waitForTimeout(20000);
    
  } catch (error) {
    console.error('Error during testing:', error);
  } finally {
    await browser.close();
    console.log('✅ Verification completed');
  }
})();