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

/**
 * Optimized trainer journey tests with better performance
 * @tag @trainer-journey @optimized
 */

test.describe('Trainer Journey - Optimized', () => {
  test('Complete trainer workflow verification', async ({ authenticatedPage: page }) => {
    test.setTimeout(45000); // Set reasonable timeout
    const actions = new CommonActions(page);
    
    // Step 1: Verify Dashboard
    console.log('Step 1: Verifying Dashboard...');
    await expect(page).toHaveURL(/hvac-dashboard/);
    await expect(page.locator('h1, h2').filter({ hasText: /dashboard/i }).first()).toBeVisible();
    await actions.verifyNavigation();
    await actions.screenshot('dashboard-verified');
    
    // Step 2: Test Create Event Page
    console.log('Step 2: Testing Create Event page...');
    await actions.navigateAndWait('/manage-event/');
    
    // Verify page loaded with more specific heading check
    const headings = await page.locator('h1, h2').all();
    let foundEventHeading = false;
    for (const heading of headings) {
      const text = await heading.textContent();
      if (text && (/create.*event|manage.*event/i.test(text))) {
        foundEventHeading = true;
        break;
      }
    }
    expect(foundEventHeading).toBeTruthy();
    
    // Test form fields
    await expect(page.locator('#event_title').first()).toBeVisible();
    await page.fill('#event_title', 'Optimized Test Event');
    
    const titleValue = await page.locator('#event_title').inputValue();
    expect(titleValue).toBe('Optimized Test Event');
    await actions.screenshot('create-event-form-filled');
    
    // Step 3: Test Certificate Reports
    console.log('Step 3: Testing Certificate Reports...');
    await actions.navigateAndWait('/certificate-reports/');
    await expect(page.locator('h1, h2').filter({ hasText: /certificate/i }).first()).toBeVisible();
    await actions.screenshot('certificate-reports-loaded');
    
    // Step 4: Test Generate Certificates
    console.log('Step 4: Testing Generate Certificates...');
    await actions.navigateAndWait('/generate-certificates/');
    await expect(page.locator('h1, h2').filter({ hasText: /generate.*certificate/i }).first()).toBeVisible();
    
    const eventSelect = page.locator('select[name="event_id"]');
    await expect(eventSelect.first()).toBeVisible();
    await actions.screenshot('generate-certificates-loaded');
    
    // Step 5: Test Profile Page
    console.log('Step 5: Testing Profile page...');
    await actions.navigateAndWait('/community-profile/');
    await expect(page.locator('h1, h2').filter({ hasText: /profile/i }).first()).toBeVisible();
    await actions.screenshot('profile-loaded');
    
    console.log('✓ Complete trainer workflow verified successfully');
  });

  test('Navigation flow between all pages', async ({ authenticatedPage: page }) => {
    test.setTimeout(30000);
    const actions = new CommonActions(page);
    
    const pages = [
      { path: '/hvac-dashboard/', name: 'Dashboard', expectedText: /dashboard/i },
      { path: '/manage-event/', name: 'Create Event', expectedText: /event/i },
      { path: '/certificate-reports/', name: 'Certificate Reports', expectedText: /certificate/i },
      { path: '/generate-certificates/', name: 'Generate Certificates', expectedText: /generate|certificate/i },
      { path: '/community-profile/', name: 'Profile', expectedText: /profile/i }
    ];
    
    for (const pageInfo of pages) {
      console.log(`Testing ${pageInfo.name}...`);
      await actions.navigateAndWait(pageInfo.path);
      
      // Verify page loaded
      await expect(page.locator('h1, h2').filter({ hasText: pageInfo.expectedText }).first()).toBeVisible();
      
      // Verify navigation is present
      await expect(page.locator('a[href*="hvac-dashboard"]').first()).toBeVisible();
      
      console.log(`✓ ${pageInfo.name} verified`);
    }
    
    console.log('✓ All page navigation verified');
  });

  test('Form functionality quick test', async ({ authenticatedPage: page }) => {
    test.setTimeout(20000);
    const actions = new CommonActions(page);
    
    // Test Create Event form
    await actions.navigateAndWait('/manage-event/');
    
    // Fill title field
    const titleField = page.locator('#event_title');
    await expect(titleField).toBeVisible();
    await titleField.fill('Quick Form Test');
    
    // Verify input
    const value = await titleField.inputValue();
    expect(value).toBe('Quick Form Test');
    
    // Check for description field (don't try to fill TinyMCE)
    const descriptionFields = page.locator('#event_content, textarea');
    const descCount = await descriptionFields.count();
    expect(descCount).toBeGreaterThan(0);
    
    console.log('✓ Form functionality verified');
  });

  test('Certificate system quick verification', async ({ authenticatedPage: page }) => {
    test.setTimeout(25000);
    const actions = new CommonActions(page);
    
    // Test Certificate Reports
    await actions.navigateAndWait('/certificate-reports/');
    await expect(page.locator('h1, h2').filter({ hasText: /certificate/i }).first()).toBeVisible();
    
    // Test Generate Certificates
    await actions.navigateAndWait('/generate-certificates/');
    await expect(page.locator('h1, h2').filter({ hasText: /generate/i }).first()).toBeVisible();
    
    // Test event selection
    const eventSelect = page.locator('select[name="event_id"]');
    await expect(eventSelect).toBeVisible();
    
    const options = await eventSelect.locator('option').count();
    expect(options).toBeGreaterThan(1);
    
    console.log(`Certificate system verified with ${options} events available`);
  });
});