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

(async () => {
  console.log('Starting master dashboard navigation color check...');
  
  // Launch browser in headless mode to capture screenshots
  const browser = await chromium.launch({
    headless: true,
    args: ['--window-size=1920,1080']
  });

  const context = await browser.newContext({
    viewport: { width: 1920, height: 1080 }
  });
  
  const page = await context.newPage();

  try {
    // Navigate to login page
    console.log('Navigating to login page...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    
    // Wait for login form
    await page.waitForSelector('#user_login', { timeout: 10000 });
    
    // Fill in login credentials for master trainer
    console.log('Logging in as master trainer...');
    await page.fill('#user_login', 'test_master');
    await page.fill('#user_pass', 'TestMaster123!');
    
    // Click login button
    await page.click('#wp-submit');
    
    // Wait for navigation to complete
    console.log('Waiting for dashboard to load...');
    await page.waitForURL('**/master-trainer/master-dashboard/**', { timeout: 15000 });
    
    // Wait for navigation menu to be visible
    await page.waitForSelector('.hvac-trainer-menu', { timeout: 10000 });
    
    // Take screenshot of the page
    const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
    const screenshotPath = `/tmp/master-dashboard-nav-${timestamp}.png`;
    
    console.log('Taking screenshot...');
    await page.screenshot({ 
      path: screenshotPath,
      fullPage: false 
    });
    
    console.log(`Screenshot saved to: ${screenshotPath}`);
    
    // Also take a screenshot focused on just the navigation
    const navScreenshotPath = `/tmp/master-nav-only-${timestamp}.png`;
    const navElement = await page.$('.hvac-trainer-menu-wrapper');
    if (navElement) {
      await navElement.screenshot({ path: navScreenshotPath });
      console.log(`Navigation screenshot saved to: ${navScreenshotPath}`);
    }
    
    // No need to keep browser open in headless mode
    
  } catch (error) {
    console.error('Error:', error);
    
    // Take error screenshot
    const errorScreenshot = `/tmp/master-nav-error-${Date.now()}.png`;
    await page.screenshot({ path: errorScreenshot });
    console.log(`Error screenshot saved to: ${errorScreenshot}`);
  } finally {
    await browser.close();
    console.log('Browser closed.');
  }
})();