const { chromium } = require('@playwright/test');

(async () => {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext();
  const page = await context.newPage();

  console.log('Testing HVAC Announcements on Staging...\n');

  try {
    // Test 1: Check if login page works
    console.log('1. Testing login page...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.waitForLoadState('networkidle', { timeout: 30000 });
    const loginTitle = await page.title();
    console.log(`   ✅ Login page loaded: ${loginTitle}`);

    // Test 2: Try logging in as master trainer
    console.log('\n2. Logging in as master trainer...');
    await page.fill('#user_login', 'test_master');
    await page.fill('#user_pass', 'TestMaster123!');
    await page.click('#wp-submit');
    await page.waitForNavigation({ timeout: 30000 });
    console.log(`   ✅ Logged in successfully`);

    // Test 3: Check if manage announcements page exists
    console.log('\n3. Checking manage announcements page...');
    await page.goto('https://upskill-staging.measurequick.com/master-trainer/manage-announcements/');
    await page.waitForLoadState('networkidle', { timeout: 30000 });
    
    // Check if the page has announcements content
    const hasAnnouncementsContent = await page.locator('.hvac-announcements-manager').count() > 0 ||
                                    await page.locator('#hvac-announcements-app').count() > 0 ||
                                    await page.locator('.announcement').count() > 0 ||
                                    await page.textContent('body').then(text => text.includes('Announcements'));
    
    if (hasAnnouncementsContent) {
      console.log('   ✅ Manage announcements page is accessible');
    } else {
      console.log('   ⚠️  Manage announcements page might not be fully configured');
    }

    // Test 4: Check trainer view announcements
    console.log('\n4. Checking trainer announcements view...');
    await page.goto('https://upskill-staging.measurequick.com/trainer/announcements/');
    await page.waitForLoadState('networkidle', { timeout: 30000 });
    
    const hasTrainerView = await page.locator('.hvac-announcements-timeline').count() > 0 ||
                           await page.locator('.hvac-announcements-list').count() > 0 ||
                           await page.textContent('body').then(text => text.includes('announcement'));
    
    if (hasTrainerView) {
      console.log('   ✅ Trainer announcements view is accessible');
    } else {
      console.log('   ⚠️  Trainer announcements view might not be configured');
    }

    console.log('\n✅ Basic announcements testing complete!');
    
  } catch (error) {
    console.error('❌ Test failed:', error.message);
  } finally {
    await browser.close();
  }
})();