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

const BASE_URL = 'https://upskill-staging.measurequick.com';

async function testTrainerFeatures() {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    viewport: { width: 1920, height: 1080 }
  });
  const page = await context.newPage();
  
  console.log('Starting trainer features test...');
  
  try {
    // Test 1: Registration Form
    console.log('\n1. Testing Registration Form...');
    await page.goto(`${BASE_URL}/trainer-registration/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/01-registration-form-full.png', 
      fullPage: true 
    });
    
    // Test venue conditional fields
    await page.click('input[name="create_venue"][value="yes"]');
    await page.waitForTimeout(500);
    await page.screenshot({ 
      path: 'screenshots/02-venue-fields-visible.png' 
    });
    
    await page.click('input[name="create_venue"][value="no"]');
    await page.waitForTimeout(500);
    await page.screenshot({ 
      path: 'screenshots/03-venue-fields-hidden.png' 
    });
    
    // Test 2: Login as trainer
    console.log('\n2. Logging in as trainer...');
    await page.goto(`${BASE_URL}/wp-login.php`);
    await page.fill('#user_login', 'test_trainer');
    await page.fill('#user_pass', 'password123');
    await page.screenshot({ 
      path: 'screenshots/04-login-page.png' 
    });
    await page.click('#wp-submit');
    await page.waitForURL('**/wp-admin/**');
    
    // Test 3: Venues List
    console.log('\n3. Testing Venues List...');
    await page.goto(`${BASE_URL}/trainer/venue/list/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/05-venues-list.png', 
      fullPage: true 
    });
    
    // Test 4: Create Venue
    console.log('\n4. Testing Create Venue...');
    await page.goto(`${BASE_URL}/trainer/venue/manage/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/06-venue-create-form.png', 
      fullPage: true 
    });
    
    // Test 5: Profile View
    console.log('\n5. Testing Profile View...');
    await page.goto(`${BASE_URL}/trainer/profile/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/07-profile-view.png', 
      fullPage: true 
    });
    
    // Test 6: Profile Edit
    console.log('\n6. Testing Profile Edit...');
    await page.goto(`${BASE_URL}/trainer/profile/edit/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/08-profile-edit.png', 
      fullPage: true 
    });
    
    // Test 7: Organizers List
    console.log('\n7. Testing Organizers List...');
    await page.goto(`${BASE_URL}/trainer/organizer/list/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/09-organizers-list.png', 
      fullPage: true 
    });
    
    // Test 8: Create Organizer
    console.log('\n8. Testing Create Organizer...');
    await page.goto(`${BASE_URL}/trainer/organizer/manage/`);
    await page.waitForLoadState('networkidle');
    await page.screenshot({ 
      path: 'screenshots/10-organizer-create-form.png', 
      fullPage: true 
    });
    
    console.log('\nAll tests completed successfully!');
    console.log('Screenshots saved in screenshots/ directory');
    
  } catch (error) {
    console.error('Test failed:', error);
    await page.screenshot({ 
      path: 'screenshots/error-screenshot.png', 
      fullPage: true 
    });
  } finally {
    await browser.close();
  }
}

// Run the tests
testTrainerFeatures().catch(console.error);