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

async function testManageEventFinal() {
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext();
    const page = await context.newPage();
    
    console.log('=== Final Test: Manage Event Page ===\n');
    
    // Login
    console.log('1. Logging in...');
    await page.goto('https://upskill-staging.measurequick.com/training-login/');
    await page.fill('#user_login', 'ben+test44@measurequick.com');
    await page.fill('#user_pass', 'MQtrainer2024!');
    await page.locator('input[type="submit"], #wp-submit').first().click();
    
    // Wait for redirect
    await page.waitForURL('**/trainer/dashboard/**', { timeout: 10000 }).catch(() => {});
    
    // Navigate to manage event page
    console.log('2. Navigating to manage event page...');
    await page.goto('https://upskill-staging.measurequick.com/trainer/event/manage/');
    await page.waitForLoadState('networkidle');
    
    // Check for HTML comments
    console.log('\n3. Checking for HTML comments...');
    const pageContent = await page.content();
    const hasWpShortcodeComment = pageContent.includes('<!-- wp:shortcode');
    const hasClosingComment = pageContent.includes('<!-- /wp:shortcode');
    
    // Check page text content
    const textContent = await page.textContent('body');
    const hasVisibleComment = textContent.includes('<!-- wp:shortcode') || textContent.includes('<\\!-- wp:shortcode');
    
    console.log(`   - HTML contains wp:shortcode comment: ${hasWpShortcodeComment ? '❌ YES' : '✅ NO'}`);
    console.log(`   - HTML contains closing comment: ${hasClosingComment ? '❌ YES' : '✅ NO'}`);
    console.log(`   - Visible text contains comment: ${hasVisibleComment ? '❌ YES' : '✅ NO'}`);
    
    // Check for form presence
    console.log('\n4. Checking for event form...');
    const hasForm = await page.locator('form#event-community-form, .tribe-community-events').count() > 0;
    console.log(`   - Event form present: ${hasForm ? '✅ YES' : '❌ NO'}`);
    
    // Check navigation
    console.log('\n5. Checking navigation...');
    const navCount = await page.locator('.hvac-dashboard-nav').count();
    console.log(`   - Navigation sections: ${navCount} ${navCount === 1 ? '✅' : '❌'}`);
    
    // Take screenshot
    await page.screenshot({ path: 'manage-event-final-test.png', fullPage: false });
    console.log('\n6. Screenshot saved as manage-event-final-test.png');
    
    // Summary
    console.log('\n=== Summary ===');
    if (!hasWpShortcodeComment && !hasClosingComment && !hasVisibleComment && hasForm && navCount === 1) {
        console.log('✅ ALL TESTS PASSED! The page is working correctly.');
    } else {
        console.log('❌ Some issues remain. Please check the details above.');
    }
    
    await browser.close();
}

testManageEventFinal().catch(console.error);