import { test, expect } from '@playwright/test';

test.describe('Plugin Status Verification', () => {
  test('verify HVAC Community Events plugin status on staging', async ({ page }) => {
    console.log('Starting plugin status verification...');

    // First check if we can access the site
    await page.goto('https://upskill-staging.measurequick.com/');
    await expect(page).toHaveTitle(/Upskill HVAC/);
    console.log('✓ Site is accessible');

    // Try to access WordPress admin
    await page.goto('https://upskill-staging.measurequick.com/wp-admin/');
    
    // Check if we're redirected to login
    await page.waitForLoadState('networkidle');
    const url = page.url();
    console.log(`Current URL: ${url}`);

    if (url.includes('wp-login')) {
      console.log('Need to login to access admin');
      
      // Try to login with admin credentials if available
      try {
        await page.fill('#user_login', 'admin');
        await page.fill('#user_pass', 'admin'); // Common staging password
        await page.click('#wp-submit');
        await page.waitForLoadState('networkidle');
        
        // Check if login was successful
        if (page.url().includes('wp-admin') && !page.url().includes('wp-login')) {
          console.log('✓ Successfully logged into admin');
          
          // Navigate to plugins page
          await page.goto('https://upskill-staging.measurequick.com/wp-admin/plugins.php');
          await page.waitForLoadState('networkidle');
          
          // Check for HVAC plugin
          const hvacPlugin = page.locator('tr[data-slug="hvac-community-events"], tr:has-text("HVAC Community Events")');
          const pluginExists = await hvacPlugin.count() > 0;
          
          if (pluginExists) {
            console.log('✓ HVAC Community Events plugin found in plugins list');
            
            // Check if it's active
            const isActive = await page.locator('tr[data-slug="hvac-community-events"] .plugin-title strong, tr:has-text("HVAC Community Events") .plugin-title strong').count() > 0;
            console.log(`Plugin active status: ${isActive}`);
            
            // Get plugin details
            const pluginRow = page.locator('tr[data-slug="hvac-community-events"], tr:has-text("HVAC Community Events")').first();
            const pluginText = await pluginRow.textContent();
            console.log(`Plugin details: ${pluginText?.substring(0, 200)}...`);
          } else {
            console.log('✗ HVAC Community Events plugin NOT found in plugins list');
            
            // List all plugins to see what's installed
            const allPlugins = await page.locator('#the-list tr .plugin-title strong').allTextContents();
            console.log('Installed plugins:', allPlugins);
          }
          
          // Check if we can access HVAC menu items
          const hvacMenu = page.locator('#adminmenu a:has-text("HVAC")');
          const menuExists = await hvacMenu.count() > 0;
          console.log(`HVAC admin menu exists: ${menuExists}`);
          
        } else {
          console.log('✗ Login failed or redirected');
        }
      } catch (error) {
        console.log('Login attempt failed:', error);
      }
    }

    // Check frontend for plugin indicators
    await page.goto('https://upskill-staging.measurequick.com/');
    await page.waitForLoadState('networkidle');
    
    // Look for HVAC-specific CSS classes, scripts, or content
    const hvacIndicators = {
      cssFiles: await page.locator('link[href*="hvac"]').count(),
      jsFiles: await page.locator('script[src*="hvac"]').count(),
      hvacClasses: await page.locator('[class*="hvac"]').count(),
      communityPages: await page.locator('a[href*="community"]').count(),
      trainerPages: await page.locator('a[href*="trainer"]').count()
    };
    
    console.log('Frontend HVAC indicators:', hvacIndicators);
    
    // Check if community registration page exists
    try {
      await page.goto('https://upskill-staging.measurequick.com/community-registration/');
      await page.waitForLoadState('networkidle');
      const pageTitle = await page.title();
      const pageExists = !pageTitle.includes('Page not found') && !pageTitle.includes('404');
      console.log(`Community registration page exists: ${pageExists}`);
      console.log(`Page title: ${pageTitle}`);
    } catch (error) {
      console.log('Community registration page not accessible');
    }
    
    // Check if trainer dashboard exists
    try {
      await page.goto('https://upskill-staging.measurequick.com/trainer-dashboard/');
      await page.waitForLoadState('networkidle');
      const pageTitle = await page.title();
      const pageExists = !pageTitle.includes('Page not found') && !pageTitle.includes('404');
      console.log(`Trainer dashboard page exists: ${pageExists}`);
      console.log(`Page title: ${pageTitle}`);
    } catch (error) {
      console.log('Trainer dashboard page not accessible');
    }
  });
});