#!/usr/bin/env node

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

(async () => {
    console.log('Checking what form is actually on the manage event page...\n');
    
    const browser = await chromium.launch({ 
        headless: true,
        args: ['--no-sandbox'] 
    });
    const page = await browser.newPage();
    
    try {
        // Login
        console.log('1. Logging in...');
        await page.goto('https://upskill-staging.measurequick.com/wp-login.php');
        await page.fill('#user_login', 'test_admin');
        await page.fill('#user_pass', 'TestAdmin2025!');
        await page.click('#wp-submit');
        await page.waitForTimeout(3000);
        
        // Go to manage event page with event_id
        console.log('2. Going to manage event page with event_id=6161...');
        await page.goto('https://upskill-staging.measurequick.com/trainer/event/manage/?event_id=6161');
        await page.waitForLoadState('networkidle');
        
        // Check what's actually on the page
        const pageContent = await page.evaluate(() => {
            const results = {};
            
            // Check for any forms
            const forms = document.querySelectorAll('form');
            results.formsCount = forms.length;
            results.formIds = Array.from(forms).map(f => f.id || '(no id)');
            
            // Check for TEC shortcode content
            const shortcodeDiv = document.querySelector('[data-tribe-shortcode]');
            results.hasTribeShortcode = !!shortcodeDiv;
            
            // Check for actual TEC form
            const tecForm = document.querySelector('#add-new-event, .tribe-community-events-content, #tribe-community-events');
            results.hasTECForm = !!tecForm;
            
            // Check page content
            const contentArea = document.querySelector('.hvac-page-content');
            if (contentArea) {
                results.contentHTML = contentArea.innerHTML.substring(0, 2000);
            }
            
            // Check for title input specifically
            const titleInput = document.querySelector('input[name="post_title"]');
            results.hasTitleInput = !!titleInput;
            results.titleValue = titleInput ? titleInput.value : null;
            
            // Check for the shortcode itself
            const shortcodeText = document.body.innerText.includes('[tribe_community_events]');
            results.hasShortcodeText = shortcodeText;
            
            return results;
        });
        
        console.log('\n3. Page Analysis:');
        console.log('   Forms found:', pageContent.formsCount);
        console.log('   Form IDs:', pageContent.formIds);
        console.log('   Has TEC shortcode div:', pageContent.hasTribeShortcode);
        console.log('   Has TEC form:', pageContent.hasTECForm);
        console.log('   Has shortcode text:', pageContent.hasShortcodeText);
        
        if (pageContent.contentHTML) {
            console.log('\n4. Content preview:');
            console.log(pageContent.contentHTML);
        }
        
    } catch (error) {
        console.error('Error:', error.message);
    } finally {
        await browser.close();
    }
})();