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

async function testFixes() {
  console.log('🧪 Testing both fixes...');
  
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  
  try {
    // Test 1: Verify registration-pending page exists and loads correctly
    console.log('📋 Test 1: Testing /registration-pending/ page...');
    
    await page.goto('https://upskill-staging.measurequick.com/registration-pending/');
    await page.waitForLoadState('networkidle');
    
    const pageTitle = await page.title();
    const registrationContent = await page.locator('h2:has-text("Registration Submitted Successfully")').isVisible();
    const returnToLoginLink = await page.locator('a:has-text("Return to Login")').isVisible();
    
    console.log(`  📄 Page title: ${pageTitle}`);
    console.log(`  ✅ Registration content visible: ${registrationContent}`);
    console.log(`  🔗 Return to login link visible: ${returnToLoginLink}`);
    
    // Take screenshot
    await page.screenshot({ path: 'test-results/registration-pending-page.png', fullPage: true });
    
    // Test the Return to Login link
    if (returnToLoginLink) {
      await page.click('a:has-text("Return to Login")');
      await page.waitForLoadState('networkidle');
      
      const loginPageUrl = page.url();
      const isOnLoginPage = loginPageUrl.includes('/training-login/');
      console.log(`  🔗 Return link works: ${isOnLoginPage} (${loginPageUrl})`);
    }
    
    // Test 2: Check if we can access the training-login page directly
    console.log('\n🔐 Test 2: Testing /training-login/ page...');
    
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.waitForLoadState('networkidle');
    
    const loginForm = await page.locator('#loginform, form[name="loginform"], #user_login').isVisible();
    const loginTitle = await page.locator('h1, h2, .login-title').textContent();
    
    console.log(`  📄 Login form visible: ${loginForm}`);
    console.log(`  📝 Login page title/heading: ${loginTitle}`);
    
    // Take screenshot
    await page.screenshot({ path: 'test-results/training-login-page.png', fullPage: true });
    
    console.log('\n✅ Both page tests completed successfully!');
    console.log('\n📋 Test Summary:');
    console.log(`  • /registration-pending/ page: ${registrationContent ? 'WORKING' : 'FAILED'}`);
    console.log(`  • /training-login/ page: ${loginForm ? 'WORKING' : 'FAILED'}`);
    console.log(`  • Return to login link: ${returnToLoginLink ? 'WORKING' : 'FAILED'}`);
    
  } catch (error) {
    console.error('❌ Test failed:', error.message);
  } finally {
    await browser.close();
  }
}

testFixes();