/**
 * Enhanced TEC Template Testing Script
 * 
 * Comprehensive E2E testing for the enhanced TEC Community Events template
 * Tests 100% field population success rate with all new WordPress fields
 */

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

// Test configuration
const config = {
    baseUrl: process.env.UPSKILL_STAGING_URL || 'https://upskill-staging.measurequick.com',
    timeout: 45000,
    testCredentials: {
        username: 'test_trainer',
        password: 'TestTrainer123!'
    },
    testEventData: {
        // Core WordPress fields
        title: 'Advanced HVAC Diagnostics Workshop - Enhanced Template Test',
        content: `<h2>Comprehensive HVAC Training Event</h2>
        
        <p>Join us for an intensive workshop covering advanced HVAC diagnostic techniques. This enhanced template test verifies all field population capabilities.</p>
        
        <h3>What You'll Learn:</h3>
        <ul>
            <li>Advanced refrigeration cycle analysis</li>
            <li>Electrical troubleshooting techniques</li>
            <li>Airflow measurement and optimization</li>
            <li>Energy efficiency assessments</li>
        </ul>
        
        <p><strong>Bring:</strong> Laptop, basic tools, and eagerness to learn!</p>`,
        
        excerpt: 'Join us for an intensive HVAC diagnostics workshop covering advanced techniques, electrical troubleshooting, and energy efficiency. Perfect for technicians looking to enhance their diagnostic skills.',
        
        // Enhanced taxonomy fields
        categories: [1, 2], // Will map to actual category IDs
        tags: ['HVAC', 'diagnostics', 'workshop', 'training', 'certification', 'energy-efficiency'],
        
        // Featured image (will use a test image)
        featured_image: {
            id: null, // Will be set during test
            url: null
        },
        
        // TEC specific fields
        venue: 'Test Training Center',
        organizer: 'Test HVAC Training Company',
        start_date: '2025-09-15',
        start_time: '09:00',
        end_date: '2025-09-15',
        end_time: '17:00',
        cost: '299'
    }
};

console.log('🧪 Enhanced TEC Template E2E Testing Suite');
console.log(`🌐 Testing URL: ${config.baseUrl}`);
console.log('📋 Testing enhanced template with 100% field population');
console.log('');

async function runEnhancedTemplateTest() {
    const browser = await chromium.launch({ 
        headless: false, // Show browser for demonstration
        slowMo: 1000 // Slow down for visual verification
    });
    
    const context = await browser.newContext({
        viewport: { width: 1920, height: 1080 }
    });
    
    const page = await context.newPage();
    
    // Enhanced console logging
    page.on('console', msg => {
        if (msg.type() === 'log' && (
            msg.text().includes('HVAC') || 
            msg.text().includes('Enhanced') ||
            msg.text().includes('Field Population')
        )) {
            console.log(`🔍 Browser: ${msg.text()}`);
        }
    });
    
    // Track errors
    const pageErrors = [];
    page.on('pageerror', error => {
        pageErrors.push(error.message);
        console.error(`❌ Page Error: ${error.message}`);
    });
    
    try {
        console.log('📋 Step 1: Login and navigate to event creation');
        
        // Navigate to login
        await page.goto(`${config.baseUrl}/training-login/`);
        await page.waitForLoadState('networkidle');
        
        // Login
        const loginSuccess = await performLogin(page, config.testCredentials);
        if (!loginSuccess) {
            throw new Error('Login failed');
        }
        
        console.log('📋 Step 2: Navigate to enhanced event creation form');
        
        // Navigate to event creation - try multiple possible URLs
        const eventCreateUrls = [
            '/trainer/event/create/',
            '/events/community/add/',
            '/community/events/add/',
            '/events/add/'
        ];
        
        let eventFormFound = false;
        for (let url of eventCreateUrls) {
            try {
                await page.goto(`${config.baseUrl}${url}`);
                await page.waitForTimeout(2000);
                
                // Check if enhanced template is active
                const enhancedIndicator = await page.querySelector('.hvac-success-indicator');
                if (enhancedIndicator) {
                    console.log(`✅ Enhanced template found at: ${url}`);
                    eventFormFound = true;
                    break;
                }
            } catch (e) {
                console.log(`ℹ️ URL ${url} not accessible, trying next...`);
            }
        }
        
        if (!eventFormFound) {
            // Try to find create event link from dashboard
            await page.goto(`${config.baseUrl}/trainer/dashboard/`);
            await page.waitForTimeout(2000);
            
            const createEventLink = await page.locator('a').filter({ hasText: /create.*event/i }).first();
            if (await createEventLink.count() > 0) {
                await createEventLink.click();
                await page.waitForLoadState('networkidle');
                eventFormFound = true;
            }
        }
        
        if (!eventFormFound) {
            throw new Error('Could not find enhanced event creation form');
        }
        
        console.log('📋 Step 3: Verify enhanced template is active');
        
        await verifyEnhancedTemplate(page);
        
        console.log('📋 Step 4: Test enhanced field population system');
        
        const populationResults = await testEnhancedFieldPopulation(page, config.testEventData);
        
        console.log('📋 Step 5: Test individual field functionality');
        
        const fieldTests = await testIndividualFields(page);
        
        console.log('📋 Step 6: Test form submission');
        
        const submissionResult = await testFormSubmission(page);
        
        console.log('📋 Step 7: Generate comprehensive test report');
        
        const testReport = generateTestReport({
            template_verification: true,
            field_population: populationResults,
            individual_fields: fieldTests,
            form_submission: submissionResult,
            page_errors: pageErrors
        });
        
        console.log('\n🎉 ENHANCED TEMPLATE TEST COMPLETE');
        console.log('='.repeat(50));
        console.log(testReport);
        
        // Take final screenshot
        await page.screenshot({ 
            path: 'test-results/enhanced-template-final.png',
            fullPage: true 
        });
        
        return testReport;
        
    } catch (error) {
        console.error('❌ Enhanced template test failed:', error.message);
        
        // Take error screenshot
        await page.screenshot({ 
            path: 'test-results/enhanced-template-error.png',
            fullPage: true 
        });
        
        throw error;
    } finally {
        await browser.close();
    }
}

async function performLogin(page, credentials) {
    try {
        // Find login form elements
        const usernameField = await page.locator('input[name="log"], #user_login, input[type="text"]').first();
        const passwordField = await page.locator('input[name="pwd"], #user_pass, input[type="password"]').first();
        const submitButton = await page.locator('input[type="submit"], button[type="submit"], .wp-submit').first();
        
        if (await usernameField.count() === 0 || await passwordField.count() === 0) {
            throw new Error('Login form not found');
        }
        
        await usernameField.fill(credentials.username);
        await passwordField.fill(credentials.password);
        await submitButton.click();
        
        await page.waitForLoadState('networkidle');
        
        // Verify login success
        const loginError = await page.locator('.login_error, .error, #login_error').first();
        if (await loginError.count() > 0) {
            const errorText = await loginError.textContent();
            throw new Error(`Login failed: ${errorText}`);
        }
        
        console.log('✅ Login successful');
        return true;
        
    } catch (error) {
        console.error('❌ Login error:', error.message);
        return false;
    }
}

async function verifyEnhancedTemplate(page) {
    console.log('🔍 Verifying enhanced template features...');
    
    const checks = {
        enhanced_indicator: '.hvac-success-indicator',
        excerpt_field: '#hvac_post_excerpt',
        categories_section: '#hvac-categories-section',
        featured_image_section: '#hvac-featured-image-section',
        tags_section: '#hvac-tags-section',
        enhanced_styles: '#hvac-tec-enhanced-styles'
    };
    
    const results = {};
    
    for (let [name, selector] of Object.entries(checks)) {
        const element = await page.locator(selector).first();
        const exists = await element.count() > 0;
        results[name] = exists;
        
        if (exists) {
            console.log(`✅ ${name}: Found`);
        } else {
            console.log(`❌ ${name}: Not found (${selector})`);
        }
    }
    
    const totalChecks = Object.keys(checks).length;
    const passedChecks = Object.values(results).filter(Boolean).length;
    const successRate = Math.round((passedChecks / totalChecks) * 100);
    
    console.log(`📊 Template verification: ${passedChecks}/${totalChecks} features found (${successRate}%)`);
    
    if (successRate < 80) {
        throw new Error(`Enhanced template verification failed: ${successRate}% < 80%`);
    }
    
    return results;
}

async function testEnhancedFieldPopulation(page, eventData) {
    console.log('🎯 Testing enhanced field population system...');
    
    // Inject test data and run population
    const populationResult = await page.evaluate((testData) => {
        // Check if enhanced system is available
        if (!window.HVACEnhancedFieldPopulation) {
            return { error: 'Enhanced field population system not found' };
        }
        
        // Run field access test first
        const accessTest = window.HVACEnhancedFieldPopulation.testFieldAccess();
        
        // Run field population
        const populationTest = window.HVACEnhancedFieldPopulation.populateAllFields(testData);
        
        return {
            access_test: accessTest,
            population_test: populationTest,
            debug_info: window.HVACEnhancedFieldPopulation.debug || {}
        };
    }, eventData);
    
    console.log('📊 Field Population Results:');
    if (populationResult.error) {
        console.error(`❌ ${populationResult.error}`);
        return populationResult;
    }
    
    const accessRate = populationResult.access_test?.access_rate || 0;
    const populationRate = populationResult.population_test?.successRate || 0;
    
    console.log(`🔍 Field Access: ${accessRate}% (${populationResult.access_test?.found_fields}/${populationResult.access_test?.total_fields})`);
    console.log(`🎯 Population Success: ${populationRate}% (${populationResult.population_test?.populated_fields}/${populationResult.population_test?.total_fields})`);
    
    // Take screenshot of populated form
    await page.screenshot({ 
        path: 'test-results/enhanced-template-populated.png',
        fullPage: true 
    });
    
    return populationResult;
}

async function testIndividualFields(page) {
    console.log('🧪 Testing individual enhanced field functionality...');
    
    const fieldTests = {};
    
    // Test excerpt field
    fieldTests.excerpt = await testExcerptField(page);
    
    // Test categories field
    fieldTests.categories = await testCategoriesField(page);
    
    // Test featured image field
    fieldTests.featured_image = await testFeaturedImageField(page);
    
    // Test tags field
    fieldTests.tags = await testTagsField(page);
    
    const totalTests = Object.keys(fieldTests).length;
    const passedTests = Object.values(fieldTests).filter(Boolean).length;
    const testSuccessRate = Math.round((passedTests / totalTests) * 100);
    
    console.log(`📊 Individual field tests: ${passedTests}/${totalTests} passed (${testSuccessRate}%)`);
    
    return fieldTests;
}

async function testExcerptField(page) {
    try {
        const excerptField = page.locator('#hvac_post_excerpt');
        if (await excerptField.count() === 0) return false;
        
        // Test typing and character counter
        await excerptField.clear();
        await excerptField.fill('Test excerpt content for enhanced template verification');
        
        // Check character counter
        const counter = page.locator('#excerpt-counter .current-count');
        if (await counter.count() > 0) {
            const count = await counter.textContent();
            console.log(`✅ Excerpt field: Character counter shows ${count}`);
        }
        
        return true;
    } catch (error) {
        console.log(`❌ Excerpt field test failed: ${error.message}`);
        return false;
    }
}

async function testCategoriesField(page) {
    try {
        const categoriesSection = page.locator('#hvac-categories-section');
        if (await categoriesSection.count() === 0) return false;
        
        // Test category selection
        const firstCategory = page.locator('.hvac-category-checkbox').first();
        if (await firstCategory.count() > 0) {
            await firstCategory.check();
            console.log('✅ Categories field: Can select categories');
        }
        
        // Test search functionality
        const searchInput = page.locator('#hvac_categories_search');
        if (await searchInput.count() > 0) {
            await searchInput.fill('test');
            await page.waitForTimeout(500);
            console.log('✅ Categories field: Search functionality working');
        }
        
        return true;
    } catch (error) {
        console.log(`❌ Categories field test failed: ${error.message}`);
        return false;
    }
}

async function testFeaturedImageField(page) {
    try {
        const imageSection = page.locator('#hvac-featured-image-section');
        if (await imageSection.count() === 0) return false;
        
        // Check if upload button exists and is clickable
        const uploadButton = page.locator('#hvac-upload-image-btn');
        if (await uploadButton.count() > 0) {
            console.log('✅ Featured image field: Upload button present');
        }
        
        return true;
    } catch (error) {
        console.log(`❌ Featured image field test failed: ${error.message}`);
        return false;
    }
}

async function testTagsField(page) {
    try {
        const tagsSection = page.locator('#hvac-tags-section');
        if (await tagsSection.count() === 0) return false;
        
        // Test adding tags
        const tagsInput = page.locator('#hvac_tags_input');
        if (await tagsInput.count() > 0) {
            await tagsInput.fill('test-tag');
            await tagsInput.press('Enter');
            
            // Check if tag was added
            const addedTag = page.locator('.hvac-tag-item');
            if (await addedTag.count() > 0) {
                console.log('✅ Tags field: Can add tags');
            }
        }
        
        return true;
    } catch (error) {
        console.log(`❌ Tags field test failed: ${error.message}`);
        return false;
    }
}

async function testFormSubmission(page) {
    try {
        // Look for submit button
        const submitButton = page.locator('input[type="submit"], button[type="submit"], .tribe-events-c-nav__list-item--publish').last();
        
        if (await submitButton.count() === 0) {
            console.log('⚠️ Form submission test skipped: Submit button not found');
            return false;
        }
        
        console.log('ℹ️ Form submission test: Submit button found but not clicking (test mode)');
        return true;
        
    } catch (error) {
        console.log(`❌ Form submission test failed: ${error.message}`);
        return false;
    }
}

function generateTestReport(results) {
    const {
        template_verification,
        field_population,
        individual_fields,
        form_submission,
        page_errors
    } = results;
    
    let report = '\n📊 ENHANCED TEC TEMPLATE TEST REPORT\n';
    report += '='.repeat(50) + '\n\n';
    
    // Template verification
    report += '🔍 TEMPLATE VERIFICATION: ';
    report += template_verification ? '✅ PASSED\n' : '❌ FAILED\n';
    
    // Field population results
    if (field_population?.population_test) {
        const pop = field_population.population_test;
        report += `🎯 FIELD POPULATION: ${pop.successRate}% (${pop.populated_fields}/${pop.total_fields})\n`;
        if (pop.successRate === 100) {
            report += '   🎉 TARGET ACHIEVED: 100% field population success!\n';
        }
    }
    
    // Field access results
    if (field_population?.access_test) {
        const access = field_population.access_test;
        report += `🔍 FIELD ACCESS: ${access.access_rate}% (${access.found_fields}/${access.total_fields})\n`;
    }
    
    // Individual field tests
    const fieldTestsCount = Object.keys(individual_fields).length;
    const fieldTestsPassed = Object.values(individual_fields).filter(Boolean).length;
    report += `🧪 INDIVIDUAL TESTS: ${fieldTestsPassed}/${fieldTestsCount} passed\n`;
    
    // Form submission
    report += '📤 FORM SUBMISSION: ';
    report += form_submission ? '✅ READY\n' : '⚠️ NEEDS CHECK\n';
    
    // Errors
    if (page_errors.length > 0) {
        report += `\n❌ PAGE ERRORS (${page_errors.length}):\n`;
        page_errors.forEach((error, index) => {
            report += `   ${index + 1}. ${error}\n`;
        });
    } else {
        report += '\n✅ NO PAGE ERRORS DETECTED\n';
    }
    
    // Overall assessment
    const overallSuccess = template_verification && 
                          (field_population?.population_test?.successRate || 0) >= 90 &&
                          fieldTestsPassed >= fieldTestsCount * 0.8;
    
    report += '\n🏆 OVERALL RESULT: ';
    report += overallSuccess ? '✅ SUCCESS' : '❌ NEEDS IMPROVEMENT';
    
    return report;
}

// Run the enhanced template test
if (require.main === module) {
    runEnhancedTemplateTest()
        .then(report => {
            console.log('\n✅ Enhanced TEC Template Test completed successfully');
            process.exit(0);
        })
        .catch(error => {
            console.error('\n❌ Enhanced TEC Template Test failed:', error.message);
            process.exit(1);
        });
}

module.exports = { runEnhancedTemplateTest };