/**
 * Test Organizer Management Functionality
 * 
 * This script tests the organizer management page to verify:
 * 1. Page loads correctly
 * 2. Shortcode is processed
 * 3. Form elements are present
 * 4. CRUD functionality works
 */

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

async function testOrganizerManagement() {
    console.log('🧪 Testing Organizer Management Functionality\n');
    
    const browser = await chromium.launch({ 
        headless: process.env.HEADLESS !== 'false',
        timeout: 30000
    });
    
    const page = await browser.newPage();
    const BASE_URL = process.env.BASE_URL || 'http://localhost:8080';
    
    try {
        console.log('📋 Test Results:');
        console.log('================');
        
        // Test 1: Check if organizer manage page exists
        console.log('\n1. Testing page accessibility...');
        const response = await page.goto(`${BASE_URL}/trainer/organizer/manage/`, { 
            waitUntil: 'networkidle',
            timeout: 15000 
        });
        
        const status = response.status();
        console.log(`   Status: ${status} ${status === 200 ? '✅' : '❌'}`);
        
        if (status === 404) {
            console.log('   ❌ Page not found - WordPress pages may not be created');
            console.log('   💡 Solution: Run HVAC_Page_Manager::create_required_pages()');
            return;
        }
        
        if (status !== 200) {
            console.log(`   ❌ Unexpected status: ${status}`);
            return;
        }
        
        // Test 2: Check for shortcode processing
        console.log('\n2. Testing shortcode processing...');
        await page.waitForTimeout(3000);
        
        const content = await page.content();
        const hasUnprocessedShortcode = content.includes('[hvac_trainer_organizer_manage]');
        const hasFormContainer = content.includes('hvac-organizer-manage');
        const hasOrgForm = content.includes('hvac-organizer-form');
        const hasOrgNameField = content.includes('org_name') || content.includes('Organization Name');
        
        console.log(`   Shortcode not processed: ${hasUnprocessedShortcode ? '❌' : '✅'}`);
        console.log(`   Has form container: ${hasFormContainer ? '✅' : '❌'}`);
        console.log(`   Has organizer form: ${hasOrgForm ? '✅' : '❌'}`);
        console.log(`   Has org name field: ${hasOrgNameField ? '✅' : '❌'}`);
        
        if (hasUnprocessedShortcode) {
            console.log('   ❌ Shortcode is not being processed');
            console.log('   💡 Check: HVAC_Organizers class instantiation and shortcode registration');
        }
        
        // Test 3: Check for required form elements
        if (hasFormContainer || hasOrgForm) {
            console.log('\n3. Testing form elements...');
            
            const requiredElements = [
                'input[name="org_name"]',
                'textarea[name="org_description"]',
                'input[name="hq_city"]',
                'input[name="hq_state"]',
                'select[name="hq_country"]',
                'input[name="org_email"]',
                'input[name="org_website"]'
            ];
            
            for (const selector of requiredElements) {
                const exists = await page.locator(selector).count() > 0;
                const fieldName = selector.replace(/input\[name="([^"]+)"\]/, '$1').replace(/textarea\[name="([^"]+)"\]/, '$1').replace(/select\[name="([^"]+)"\]/, '$1');
                console.log(`   ${fieldName}: ${exists ? '✅' : '❌'}`);
            }
        } else {
            console.log('\n3. ❌ Cannot test form elements - form not found');
        }
        
        // Test 4: Check for CSS and JS assets
        console.log('\n4. Testing assets...');
        
        const cssLoaded = content.includes('hvac-organizers.css') || content.includes('hvac-organizers-style');
        const jsLoaded = content.includes('hvac-organizers.js');
        
        console.log(`   CSS loaded: ${cssLoaded ? '✅' : '❌'}`);
        console.log(`   JS loaded: ${jsLoaded ? '✅' : '❌'}`);
        
        // Test 5: Check for navigation and breadcrumbs
        console.log('\n5. Testing navigation...');
        
        const hasNavigation = content.includes('hvac-menu') || content.includes('trainer-menu');
        const hasBreadcrumbs = content.includes('breadcrumb') || content.includes('hvac-breadcrumb');
        
        console.log(`   Has navigation: ${hasNavigation ? '✅' : '❌'}`);
        console.log(`   Has breadcrumbs: ${hasBreadcrumbs ? '✅' : '❌'}`);
        
        // Summary
        console.log('\n📊 Summary:');
        console.log('===========');
        
        const tests = [
            { name: 'Page accessible', passed: status === 200 },
            { name: 'Shortcode processed', passed: !hasUnprocessedShortcode },
            { name: 'Form container present', passed: hasFormContainer || hasOrgForm },
            { name: 'Form fields present', passed: hasOrgNameField },
            { name: 'Assets loaded', passed: cssLoaded && jsLoaded }
        ];
        
        const passedTests = tests.filter(t => t.passed).length;
        const totalTests = tests.length;
        
        console.log(`\nPassed: ${passedTests}/${totalTests} tests`);
        
        if (passedTests === totalTests) {
            console.log('🎉 All tests passed! Organizer management is working correctly.');
        } else {
            console.log('⚠️  Some tests failed. Issues found:');
            tests.filter(t => !t.passed).forEach(t => {
                console.log(`   - ${t.name}`);
            });
        }
        
        // Get page title for reference
        const title = await page.title();
        console.log(`\nPage title: "${title}"`);
        
    } catch (error) {
        console.error('\n❌ Test failed with error:', error.message);
        
        if (error.message.includes('ERR_CONNECTION_REFUSED')) {
            console.log('💡 Solution: Start the testing environment with: docker compose -f tests/docker-compose.test.yml up -d');
        }
    } finally {
        await browser.close();
    }
}

// Run the test
testOrganizerManagement().catch(console.error);