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

(async () => {
    console.log('🔍 Testing Staging Shortcode Status (Current State)');
    
    const browser = await chromium.launch({ headless: true });
    const context = await browser.newContext();
    const page = await context.newPage();

    try {
        // Test credentials
        const baseUrl = 'https://upskill-staging.measurequick.com';
        const username = 'test_trainer';
        const password = 'TestTrainer123!';

        console.log('📝 Logging in...');
        await page.goto(`${baseUrl}/trainer/login/`);
        await page.waitForSelector('#user_login', { timeout: 10000 });
        
        await page.fill('#user_login', username);
        await page.fill('#user_pass', password);
        await page.click('#wp-submit');
        
        await page.waitForURL('**/trainer/dashboard/**', { timeout: 15000 });
        console.log('✅ Login successful\n');

        // Test Create Event Page
        console.log('🧪 Testing Create Event Page');
        await page.goto(`${baseUrl}/trainer/create-event/`);
        await page.waitForTimeout(3000);

        const pageContent = await page.textContent('body');
        const pageHTML = await page.content();
        
        console.log('📊 Page Analysis:');
        console.log(`   - Page contains "Create New Event": ${pageContent.includes('Create New Event')}`);
        console.log(`   - Page contains "Event management requires": ${pageContent.includes('Event management requires')}`);
        console.log(`   - Page contains "tribe_community_events": ${pageHTML.includes('tribe_community_events')}`);
        console.log(`   - Page contains TEC form elements: ${pageHTML.includes('tribe-events') || pageHTML.includes('Event Title') || pageHTML.includes('event-form')}`);
        console.log(`   - Page contains shortcode debug: ${pageHTML.includes('hvac_create_event') || pageHTML.includes('HVAC_Shortcodes')}`);
        
        // Check console for any relevant messages
        const consoleLogs = [];
        page.on('console', msg => {
            if (msg.text().includes('shortcode') || msg.text().includes('tribe') || msg.text().includes('HVAC')) {
                consoleLogs.push(msg.text());
            }
        });

        // Check for specific error messages
        const hasPluginError = pageContent.includes('plugin is required but not active') ||
                               pageContent.includes('Community Events add-on');
        
        console.log(`   - Plugin error messages: ${hasPluginError}`);
        
        // Look for specific content patterns
        const contentLength = pageContent.length;
        console.log(`   - Total page content length: ${contentLength} characters`);
        
        if (contentLength < 1000) {
            console.log('   ⚠️  Very short page content - possible redirect or error');
        }

        // Take screenshot for manual review
        await page.screenshot({ path: 'staging-create-event-current-state.png', fullPage: true });
        console.log('   📸 Screenshot saved: staging-create-event-current-state.png');

        // Test Edit Event Page
        console.log('\n🧪 Testing Edit Event Page');
        await page.goto(`${baseUrl}/trainer/edit-event/`);
        await page.waitForTimeout(3000);

        const editPageContent = await page.textContent('body');
        console.log('📊 Edit Page Analysis:');
        console.log(`   - Page contains "Edit Event": ${editPageContent.includes('Edit Event')}`);
        console.log(`   - Page contains "No event specified": ${editPageContent.includes('No event specified') || editPageContent.includes('Please select an event')}`);
        
        await page.screenshot({ path: 'staging-edit-event-current-state.png', fullPage: true });
        console.log('   📸 Screenshot saved: staging-edit-event-current-state.png');

        console.log('\n📝 Summary:');
        console.log('='.repeat(50));
        
        if (consoleLogs.length > 0) {
            console.log('Console messages:');
            consoleLogs.forEach(log => console.log(`   - ${log}`));
        } else {
            console.log('No relevant console messages captured');
        }

    } catch (error) {
        console.error('❌ Test failed:', error.message);
    } finally {
        await browser.close();
    }
})();