import { test, expect } from '@playwright/test';
import { STAGING_URL } from './config/staging-config';

/**
 * Simple Master Dashboard test to verify functionality
 */

test.describe('Master Dashboard Simple Test', () => {
  
  test('Direct Master Dashboard access with admin user', async ({ page }) => {
    test.setTimeout(60000);
    
    // Navigate to WordPress login page directly
    await page.goto('https://upskill-staging.measurequick.com/wp-login.php');
    
    // Use admin credentials - these should work
    await page.fill('#user_login', 'upskilldevadmin');
    await page.fill('#user_pass', 'Uberrxmprk@321');
    await page.click('#wp-submit');
    
    // Wait for login to complete
    await page.waitForLoadState('networkidle');
    
    // Take screenshot after login
    await page.screenshot({ 
      path: `test-results/screenshots/admin-after-login-${Date.now()}.png`,
      fullPage: true 
    });
    
    // Navigate directly to master dashboard
    await page.goto('https://upskill-staging.measurequick.com/master-dashboard/');
    await page.waitForLoadState('networkidle');
    
    // Take screenshot of master dashboard
    await page.screenshot({ 
      path: `test-results/screenshots/master-dashboard-final-${Date.now()}.png`,
      fullPage: true 
    });
    
    // Check what we see
    const title = await page.title();
    console.log('Page title:', title);
    
    const h1Elements = await page.locator('h1').count();
    console.log('Number of h1 elements:', h1Elements);
    
    if (h1Elements > 0) {
      const h1Text = await page.locator('h1').first().textContent();
      console.log('First h1 text:', h1Text);
    }
    
    // Look for dashboard content
    const dashboardStats = await page.locator('.hvac-dashboard-stats, .dashboard-stats, .stats-grid').count();
    console.log('Dashboard stats sections found:', dashboardStats);
    
    // Check for master dashboard specific content
    const systemOverview = await page.locator('text=System Overview').count();
    console.log('System Overview text found:', systemOverview);
    
    // Verify we're not on a login or error page
    const loginForm = await page.locator('#loginform').count();
    expect(loginForm).toBe(0);
    
    // Expect to find some dashboard content
    expect(h1Elements).toBeGreaterThan(0);
  });
  
  test('Check master trainer user capabilities', async ({ page }) => {
    test.setTimeout(60000);
    
    // Login as master trainer with SSH to check capabilities
    const checkCapabilities = `
      wp user meta get master_trainer wp_capabilities --format=json | jq .
      wp user list --role=hvac_master_trainer --fields=ID,user_login,display_name
      wp cap list hvac_master_trainer
    `;
    
    console.log('Checking master trainer capabilities via SSH...');
    
    // Navigate to login page
    await page.goto('https://upskill-staging.measurequick.com/wp-login.php');
    
    // Try logging in as master trainer
    await page.fill('#user_login', 'master_trainer');
    await page.fill('#user_pass', 'MasterTrainer#2025!');
    await page.click('#wp-submit');
    
    await page.waitForLoadState('networkidle');
    
    // Check where we ended up
    const currentUrl = page.url();
    console.log('Current URL after login:', currentUrl);
    
    await page.screenshot({ 
      path: `test-results/screenshots/master-trainer-login-result-${Date.now()}.png`,
      fullPage: true 
    });
    
    // If login successful, try to access master dashboard
    if (!currentUrl.includes('wp-login')) {
      await page.goto('https://upskill-staging.measurequick.com/master-dashboard/');
      await page.waitForLoadState('networkidle');
      
      const finalUrl = page.url();
      console.log('Final URL after navigation:', finalUrl);
      
      await page.screenshot({ 
        path: `test-results/screenshots/master-trainer-dashboard-attempt-${Date.now()}.png`,
        fullPage: true 
      });
    }
  });
});