import { test, expect } from '@playwright/test';
import { CommonActions } from './utils/common-actions';

/**
 * Trainer Registration Page Fix Test
 */

test.describe('Trainer Registration Page Tests', () => {
  
  test('Check trainer registration page content', async ({ page }) => {
    test.setTimeout(20000);
    const actions = new CommonActions(page);
    
    // Navigate to trainer registration page
    await page.goto('https://upskill-staging.measurequick.com/trainer-registration/');
    await page.waitForLoadState('networkidle');
    
    await actions.screenshot('trainer-registration-page');
    
    const url = page.url();
    console.log('Trainer Registration URL:', url);
    
    // Check for page content
    const bodyText = await page.textContent('body');
    console.log('Page contains shortcode text:', bodyText.includes('[') ? '⚠ Shortcode visible' : '✓ No visible shortcodes');
    
    // Check for registration form elements
    const hasForm = await page.locator('form').count() > 0;
    const hasInputs = await page.locator('input[type="text"], input[type="email"], input[type="password"]').count() > 0;
    
    console.log('Registration form present:', hasForm ? '✓' : '✗');
    console.log('Form inputs present:', hasInputs ? '✓' : '✗');
    
    // Check for specific registration form fields
    const expectedFields = [
      'username', 'user_login', 'email', 'user_email', 
      'password', 'user_pass', 'first_name', 'last_name'
    ];
    
    let fieldCount = 0;
    for (const field of expectedFields) {
      const fieldExists = await page.locator(`input[name="${field}"], input[id="${field}"]`).count() > 0;
      if (fieldExists) fieldCount++;
    }
    
    console.log(`Registration form fields found: ${fieldCount}/${expectedFields.length}`);
    
    // Check for shortcode content vs rendered form
    const hasShortcodeBrackets = bodyText.includes('[hvac_trainer_registration]');
    console.log('Raw shortcode visible:', hasShortcodeBrackets ? '⚠ YES - Issue detected' : '✓ NO - Properly rendered');
    
    // Look for WordPress form elements
    const hasWPElements = await page.locator('.wp-block, .entry-content, .ast-container').count() > 0;
    console.log('WordPress structure:', hasWPElements ? '✓' : '✗');
    
    if (hasShortcodeBrackets) {
      console.log('🔍 ISSUE FOUND: Raw shortcode [hvac_trainer_registration] is visible on page');
      console.log('📝 This indicates the shortcode is not being processed properly');
    }
  });
  
  test('Check for specific shortcode processing issues', async ({ page }) => {
    test.setTimeout(15000);
    const actions = new CommonActions(page);
    
    await page.goto('https://upskill-staging.measurequick.com/trainer-registration/');
    await page.waitForLoadState('networkidle');
    
    // Get the full HTML to analyze
    const htmlContent = await page.content();
    
    // Check for various shortcode patterns
    const shortcodePatterns = [
      '[hvac_trainer_registration]',
      '<!-- wp:shortcode -->',
      '[tribe_community_events',
      'shortcode'
    ];
    
    console.log('\n=== Shortcode Analysis ===');
    for (const pattern of shortcodePatterns) {
      const found = htmlContent.includes(pattern);
      console.log(`Pattern "${pattern}": ${found ? '⚠ FOUND' : '✓ Not found'}`);
    }
    
    // Check if the page is loading through correct template
    const hasCustomTemplate = htmlContent.includes('hvac') || htmlContent.includes('trainer');
    console.log(`Custom template indicators: ${hasCustomTemplate ? '✓ Present' : '✗ Missing'}`);
    
    await actions.screenshot('trainer-registration-analysis');
  });
});