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

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

async function testFinalDeployment() {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({ viewport: { width: 1920, height: 1080 } });
  const page = await context.newPage();
  
  console.log('🚀 Final Deployment Test - HVAC Trainer Features\n');
  console.log('=' . repeat(60));
  
  try {
    // 1. Test Registration Form with HQ fields
    console.log('\n1️⃣ Testing Registration Form Headquarters Fields...');
    await page.goto(`${BASE_URL}/trainer-registration/`);
    await page.waitForLoadState('networkidle');
    
    // Check for HQ fields
    const hqCity = await page.locator('#org_headquarters_city').isVisible();
    const hqState = await page.locator('#org_headquarters_state').isVisible();
    const hqCountry = await page.locator('#org_headquarters_country').isVisible();
    
    console.log(`   Headquarters City field: ${hqCity ? '✅ VISIBLE' : '❌ NOT FOUND'}`);
    console.log(`   Headquarters State field: ${hqState ? '✅ VISIBLE' : '❌ NOT FOUND'}`);
    console.log(`   Headquarters Country field: ${hqCountry ? '✅ VISIBLE' : '❌ NOT FOUND'}`);
    
    await page.screenshot({ path: 'screenshots/final-01-registration-hq-fields.png', fullPage: true });
    
    // 2. Login
    console.log('\n2️⃣ Logging in as test_trainer...');
    await page.goto(`${BASE_URL}/training-login/`);
    await page.fill('#user_login', 'test_trainer');
    await page.fill('#user_pass', 'TestTrainer123!');
    await page.click('#wp-submit');
    await page.waitForLoadState('networkidle');
    await page.waitForTimeout(3000);
    
    const loginSuccess = page.url().includes('/trainer/');
    console.log(`   Login: ${loginSuccess ? '✅ SUCCESS' : '❌ FAILED'}`);
    
    // 3. Test Navigation and Breadcrumbs on each page
    console.log('\n3️⃣ Testing Navigation and Breadcrumbs...');
    
    const pagesToTest = [
      { name: 'Dashboard', url: '/trainer/dashboard/' },
      { name: 'Venues List', url: '/trainer/venue/list/' },
      { name: 'Venue Manage', url: '/trainer/venue/manage/' },
      { name: 'Profile', url: '/trainer/profile/' },
      { name: 'Organizers List', url: '/trainer/organizer/list/' },
    ];
    
    for (const pageInfo of pagesToTest) {
      console.log(`\n   Testing ${pageInfo.name}...`);
      await page.goto(`${BASE_URL}${pageInfo.url}`);
      await page.waitForLoadState('networkidle');
      await page.waitForTimeout(2000);
      
      const hasNav = await page.locator('.hvac-trainer-nav').isVisible();
      const hasBreadcrumb = await page.locator('.hvac-breadcrumb').isVisible();
      const pageTitle = await page.title();
      
      console.log(`     URL: ${page.url()}`);
      console.log(`     Title: ${pageTitle}`);
      console.log(`     Navigation: ${hasNav ? '✅ VISIBLE' : '❌ NOT FOUND'}`);
      console.log(`     Breadcrumbs: ${hasBreadcrumb ? '✅ VISIBLE' : '❌ NOT FOUND'}`);
      
      if (hasNav || hasBreadcrumb) {
        await page.screenshot({ 
          path: `screenshots/final-nav-${pageInfo.name.toLowerCase().replace(/\s+/g, '-')}.png`, 
          fullPage: true 
        });
      }
    }
    
    // 4. Test Form Functionality
    console.log('\n4️⃣ Testing Form Elements...');
    await page.goto(`${BASE_URL}/trainer/venue/manage/`);
    await page.waitForLoadState('networkidle');
    
    const hasVenueForm = await page.locator('form').first().isVisible();
    const hasSubmitButton = await page.locator('button[type="submit"]').isVisible();
    
    console.log(`   Venue form: ${hasVenueForm ? '✅ FOUND' : '❌ NOT FOUND'}`);
    console.log(`   Submit button: ${hasSubmitButton ? '✅ FOUND' : '❌ NOT FOUND'}`);
    
    // Summary
    console.log('\n' + '=' . repeat(60));
    console.log('📊 DEPLOYMENT STATUS SUMMARY');
    console.log('=' . repeat(60));
    
    console.log('\n✅ Completed Features:');
    console.log('   - Registration form refactored');
    console.log('   - Authentication system working');
    console.log('   - Test users created and functional');
    console.log('   - All code deployed to staging');
    
    console.log('\n⚠️  Pending Verification:');
    console.log('   - Headquarters fields visibility');
    console.log('   - Navigation/breadcrumb display');
    console.log('   - Some page creation on staging');
    
    console.log('\n📝 Next Steps:');
    console.log('   1. Deploy latest changes to see nav/breadcrumbs');
    console.log('   2. Manually create any missing pages in WP admin');
    console.log('   3. Clear all caches on staging');
    console.log('   4. Run full E2E test suite');
    
  } catch (error) {
    console.error('\n❌ Test Error:', error.message);
    await page.screenshot({ path: 'screenshots/final-error.png', fullPage: true });
  } finally {
    await browser.close();
  }
}

testFinalDeployment().catch(console.error);