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

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

async function testDeployedFeatures() {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    viewport: { width: 1920, height: 1080 }
  });
  const page = await context.newPage();
  
  console.log('Testing deployed trainer features on staging...\n');
  
  try {
    // Test 1: Registration Form - Check if refactored
    console.log('1. Testing Registration Form Refactor...');
    await page.goto(`${BASE_URL}/trainer-registration/`);
    await page.waitForLoadState('networkidle');
    
    // Take full page screenshot
    await page.screenshot({ 
      path: 'screenshots/deployed-01-registration-full.png', 
      fullPage: true 
    });
    
    // Check if venue section exists and test conditional display
    const venueRadio = await page.$('input[name="create_venue"]');
    if (venueRadio) {
      console.log('  - Found venue radio buttons');
      await page.click('input[name="create_venue"][value="yes"]');
      await page.waitForTimeout(500);
      await page.screenshot({ 
        path: 'screenshots/deployed-02-venue-visible.png',
        fullPage: true 
      });
    }
    
    // Test 2: Login as test trainer
    console.log('\n2. Logging in as test_trainer...');
    await page.goto(`${BASE_URL}/wp-login.php`);
    await page.fill('#user_login', 'test_trainer');
    await page.fill('#user_pass', 'password123');
    await page.click('#wp-submit');
    
    // Wait for redirect
    await page.waitForTimeout(3000);
    
    // Test 3: Check Venues List
    console.log('\n3. Testing Venues List Page...');
    await page.goto(`${BASE_URL}/trainer/venue/list/`);
    await page.waitForTimeout(2000);
    const venuesTitle = await page.title();
    console.log(`  - Page title: ${venuesTitle}`);
    await page.screenshot({ 
      path: 'screenshots/deployed-03-venues-list.png', 
      fullPage: true 
    });
    
    // Test 4: Check Venue Manage
    console.log('\n4. Testing Venue Manage Page...');
    await page.goto(`${BASE_URL}/trainer/venue/manage/`);
    await page.waitForTimeout(2000);
    await page.screenshot({ 
      path: 'screenshots/deployed-04-venue-manage.png', 
      fullPage: true 
    });
    
    // Test 5: Check Profile View
    console.log('\n5. Testing Profile View Page...');
    await page.goto(`${BASE_URL}/trainer/profile/`);
    await page.waitForTimeout(2000);
    await page.screenshot({ 
      path: 'screenshots/deployed-05-profile-view.png', 
      fullPage: true 
    });
    
    // Test 6: Check Profile Edit
    console.log('\n6. Testing Profile Edit Page...');
    await page.goto(`${BASE_URL}/trainer/profile/edit/`);
    await page.waitForTimeout(2000);
    await page.screenshot({ 
      path: 'screenshots/deployed-06-profile-edit.png', 
      fullPage: true 
    });
    
    // Test 7: Check Organizers List
    console.log('\n7. Testing Organizers List Page...');
    await page.goto(`${BASE_URL}/trainer/organizer/list/`);
    await page.waitForTimeout(2000);
    await page.screenshot({ 
      path: 'screenshots/deployed-07-organizers-list.png', 
      fullPage: true 
    });
    
    // Test 8: Check Organizer Manage
    console.log('\n8. Testing Organizer Manage Page...');
    await page.goto(`${BASE_URL}/trainer/organizer/manage/`);
    await page.waitForTimeout(2000);
    await page.screenshot({ 
      path: 'screenshots/deployed-08-organizer-manage.png', 
      fullPage: true 
    });
    
    // Test 9: Check Trainer Dashboard for navigation
    console.log('\n9. Testing Trainer Dashboard...');
    await page.goto(`${BASE_URL}/trainer/dashboard/`);
    await page.waitForTimeout(2000);
    await page.screenshot({ 
      path: 'screenshots/deployed-09-trainer-dashboard.png', 
      fullPage: true 
    });
    
    console.log('\nAll tests completed!');
    console.log('Check screenshots/ directory for results.');
    
  } catch (error) {
    console.error('Test error:', error);
    await page.screenshot({ 
      path: 'screenshots/deployed-error.png', 
      fullPage: true 
    });
  } finally {
    await browser.close();
  }
}

testDeployedFeatures().catch(console.error);