#!/usr/bin/env node

/**
 * FINAL EDIT WORKFLOW TEST
 * Tests the complete event edit workflow with the test_admin account
 * Verifies ALL fields populate and changes persist
 */

const { chromium } = require('@playwright/test');
const fs = require('fs').promises;
const { execSync } = require('child_process');

// Configure XWayland display
process.env.DISPLAY = ':0';
try {
    const xauthFile = execSync('ls /run/user/1000/.mutter-Xwaylandauth.* 2>/dev/null | head -n1', { encoding: 'utf8' }).trim();
    if (xauthFile) {
        process.env.XAUTHORITY = xauthFile;
    }
} catch (e) {
    // Continue without XAUTHORITY
}

const CONFIG = {
    baseUrl: 'https://upskill-staging.measurequick.com',
    credentials: {
        // Test admin account created by our script
        username: 'test_admin',
        password: 'TestAdmin2025!'
    }
};

async function screenshot(page, name) {
    await fs.mkdir('screenshots/final-workflow', { recursive: true });
    const path = `screenshots/final-workflow/${name}-${Date.now()}.png`;
    await page.screenshot({ path, fullPage: true });
    console.log(`📸 Screenshot: ${name}`);
    return path;
}

async function runFinalEditWorkflowTest() {
    console.log('🎯 FINAL EDIT WORKFLOW TEST');
    console.log('=' .repeat(70));
    console.log('Testing with test_admin account and seeded events');
    console.log('=' .repeat(70));
    
    const browser = await chromium.launch({ 
        headless: false,
        args: ['--no-sandbox', '--disable-setuid-sandbox']
    });
    
    const page = await browser.newPage();
    
    const results = {
        loginSuccess: false,
        eventsFound: 0,
        fieldsChecked: {},
        fieldsPopulated: 0,
        totalFields: 0,
        changesAttempted: 0,
        changesPersisted: 0
    };
    
    try {
        // 1. LOGIN
        console.log('\n📝 STEP 1: Login as test_admin');
        console.log('-' .repeat(50));
        await page.goto(`${CONFIG.baseUrl}/wp-login.php`);
        await page.waitForLoadState('domcontentloaded');
        
        await page.fill('#user_login', CONFIG.credentials.username);
        await page.fill('#user_pass', CONFIG.credentials.password);
        console.log(`  Username: ${CONFIG.credentials.username}`);
        console.log(`  Password: ${CONFIG.credentials.password}`);
        
        await screenshot(page, '01-login-form');
        
        await page.click('#wp-submit');
        
        // Wait for redirect
        try {
            await page.waitForURL('**/wp-admin/**', { timeout: 15000 });
            results.loginSuccess = true;
            console.log('✅ Logged in successfully');
        } catch (e) {
            const currentUrl = page.url();
            if (currentUrl.includes('wp-admin') || currentUrl.includes('dashboard')) {
                results.loginSuccess = true;
                console.log('✅ Logged in successfully');
            } else {
                console.log(`❌ Login failed - Current URL: ${currentUrl}`);
                return results;
            }
        }
        
        await screenshot(page, '02-dashboard');
        
        // 2. ACCESS EVENTS LIST
        console.log('\n📝 STEP 2: Access Events List');
        console.log('-' .repeat(50));
        await page.goto(`${CONFIG.baseUrl}/wp-admin/edit.php?post_type=tribe_events`);
        await page.waitForLoadState('networkidle');
        await screenshot(page, '03-events-list');
        
        // Count events
        const eventRows = await page.$$('tbody#the-list tr');
        results.eventsFound = eventRows.length;
        console.log(`✅ Found ${results.eventsFound} events`);
        
        if (results.eventsFound === 0) {
            console.log('❌ No events found to edit');
            return results;
        }
        
        // List event titles
        console.log('\nEvent titles:');
        for (let i = 0; i < Math.min(5, eventRows.length); i++) {
            try {
                const title = await eventRows[i].$eval('.row-title', el => el.textContent);
                console.log(`  ${i+1}. ${title}`);
            } catch (e) {
                // Skip if can't get title
            }
        }
        
        // 3. OPEN FIRST EVENT FOR EDITING
        console.log('\n📝 STEP 3: Open Event for Editing');
        console.log('-' .repeat(50));
        
        // Click edit on first event
        const editLink = await page.$('tbody#the-list tr:first-child .row-actions .edit a');
        if (!editLink) {
            console.log('❌ Edit link not found');
            return results;
        }
        
        await editLink.click();
        await page.waitForLoadState('networkidle');
        console.log('✅ Edit form opened');
        await screenshot(page, '04-edit-form');
        
        // 4. CHECK ALL FIELDS
        console.log('\n📝 STEP 4: Checking ALL Event Fields');
        console.log('-' .repeat(50));
        console.log('Checking field population...\n');
        
        // Define ALL fields to check
        const allFields = {
            // Basic Information
            'Title': '#title',
            'Content': '#content',
            'Excerpt': '#excerpt',
            
            // Date & Time Fields
            'Start Date': '#EventStartDate',
            'End Date': '#EventEndDate',
            'Start Time': '#EventStartTime',
            'End Time': '#EventEndTime',
            'All Day': '#EventAllDay',
            'Timezone': '#event-timezone, select[name="EventTimezone"]',
            
            // Cost Fields
            'Cost': '#EventCost',
            'Currency Symbol': '#EventCurrencySymbol',
            'Currency Position': 'select[name="EventCurrencyPosition"]',
            
            // Event Details
            'Website URL': '#EventURL',
            'Show Map': '#EventShowMap',
            'Show Map Link': '#EventShowMapLink',
            
            // Venue Fields (if using separate fields)
            'Venue Name': 'input[name="venue[Venue]"], #venue-name',
            'Venue Address': 'input[name="venue[Address]"], #VenueAddress',
            'Venue City': 'input[name="venue[City]"], #VenueCity',
            'Venue State': 'input[name="venue[State]"], #VenueState, input[name="venue[Province]"]',
            'Venue Zip': 'input[name="venue[Zip]"], #VenueZip',
            'Venue Country': 'select[name="venue[Country]"], #VenueCountry',
            'Venue Phone': 'input[name="venue[Phone]"], #VenuePhone',
            'Venue Website': 'input[name="venue[URL]"], #VenueURL',
            
            // Organizer Fields
            'Organizer Name': 'input[name="organizer[Organizer]"], #organizer-name',
            'Organizer Email': 'input[name="organizer[Email]"], #OrganizerEmail',
            'Organizer Phone': 'input[name="organizer[Phone]"], #OrganizerPhone',
            'Organizer Website': 'input[name="organizer[Website]"], #OrganizerWebsite',
            
            // Linked IDs (hidden fields)
            'Venue ID': '#EventVenueID, input[name="venue[VenueID]"]',
            'Organizer ID': '#EventOrganizerID, input[name="organizer[OrganizerID]"]'
        };
        
        results.totalFields = Object.keys(allFields).length;
        
        // Check each field
        for (const [fieldName, selector] of Object.entries(allFields)) {
            try {
                const element = await page.$(selector);
                if (element) {
                    // Try to get value based on element type
                    let value = null;
                    
                    // Check if it's a checkbox
                    if (await element.evaluate(el => el.type === 'checkbox')) {
                        value = await element.isChecked();
                    } 
                    // Try to get input value
                    else {
                        value = await element.inputValue().catch(() => null);
                    }
                    
                    // If no value, try text content
                    if (!value) {
                        value = await element.textContent().catch(() => null);
                    }
                    
                    results.fieldsChecked[fieldName] = value;
                    
                    if (value && value !== '' && value !== false) {
                        console.log(`  ✅ ${fieldName}: ${String(value).substring(0, 50)}`);
                        results.fieldsPopulated++;
                    } else {
                        console.log(`  ❌ ${fieldName}: EMPTY`);
                    }
                } else {
                    console.log(`  ⚠️ ${fieldName}: Field not found`);
                    results.fieldsChecked[fieldName] = 'NOT_FOUND';
                }
            } catch (e) {
                console.log(`  ⚠️ ${fieldName}: Error checking field`);
                results.fieldsChecked[fieldName] = 'ERROR';
            }
        }
        
        console.log('\n' + '-' .repeat(50));
        console.log(`FIELD POPULATION SUMMARY: ${results.fieldsPopulated}/${results.totalFields} fields populated`);
        
        // 5. MAKE TEST EDITS
        console.log('\n📝 STEP 5: Making Test Edits');
        console.log('-' .repeat(50));
        
        const originalValues = {
            title: results.fieldsChecked['Title'],
            cost: results.fieldsChecked['Cost'],
            startDate: results.fieldsChecked['Start Date'],
            url: results.fieldsChecked['Website URL']
        };
        
        // Edit Title
        if (originalValues.title) {
            const newTitle = originalValues.title + ' (EDITED BY TEST)';
            await page.fill('#title', newTitle);
            results.changesAttempted++;
            console.log(`  ✓ Title changed to: ${newTitle}`);
        }
        
        // Edit Cost
        await page.fill('#EventCost', '777');
        results.changesAttempted++;
        console.log('  ✓ Cost changed to: 777');
        
        // Edit Start Date
        await page.fill('#EventStartDate', '2025-12-25');
        results.changesAttempted++;
        console.log('  ✓ Start Date changed to: 2025-12-25');
        
        // Edit URL
        await page.fill('#EventURL', 'https://test-edited.example.com');
        results.changesAttempted++;
        console.log('  ✓ URL changed to: https://test-edited.example.com');
        
        await screenshot(page, '05-after-edits');
        
        // 6. SAVE CHANGES
        console.log('\n📝 STEP 6: Saving Changes');
        console.log('-' .repeat(50));
        
        const publishButton = await page.$('#publish');
        if (publishButton) {
            await publishButton.click();
            console.log('  Clicked save button...');
            
            try {
                await page.waitForSelector('.notice-success, #message', { timeout: 10000 });
                console.log('✅ Changes saved successfully');
            } catch (e) {
                console.log('⚠️ Save confirmation not detected');
            }
            
            await screenshot(page, '06-after-save');
        }
        
        // 7. VERIFY PERSISTENCE
        console.log('\n📝 STEP 7: Verifying Persistence');
        console.log('-' .repeat(50));
        console.log('Reloading page to check if changes persisted...\n');
        
        await page.reload();
        await page.waitForLoadState('networkidle');
        await screenshot(page, '07-after-reload');
        
        // Check if changes persisted
        const afterReload = {
            title: await page.$eval('#title', el => el.value).catch(() => ''),
            cost: await page.$eval('#EventCost', el => el.value).catch(() => ''),
            startDate: await page.$eval('#EventStartDate', el => el.value).catch(() => ''),
            url: await page.$eval('#EventURL', el => el.value).catch(() => '')
        };
        
        // Check Title
        if (afterReload.title.includes('(EDITED BY TEST)')) {
            console.log('  ✅ Title change PERSISTED');
            results.changesPersisted++;
        } else {
            console.log(`  ❌ Title change NOT persisted (current: ${afterReload.title})`);
        }
        
        // Check Cost
        if (afterReload.cost === '777') {
            console.log('  ✅ Cost change PERSISTED');
            results.changesPersisted++;
        } else {
            console.log(`  ❌ Cost change NOT persisted (current: ${afterReload.cost})`);
        }
        
        // Check Date
        if (afterReload.startDate === '2025-12-25') {
            console.log('  ✅ Date change PERSISTED');
            results.changesPersisted++;
        } else {
            console.log(`  ❌ Date change NOT persisted (current: ${afterReload.startDate})`);
        }
        
        // Check URL
        if (afterReload.url === 'https://test-edited.example.com') {
            console.log('  ✅ URL change PERSISTED');
            results.changesPersisted++;
        } else {
            console.log(`  ❌ URL change NOT persisted (current: ${afterReload.url})`);
        }
        
    } catch (error) {
        console.error('\n❌ Error:', error.message);
        await screenshot(page, 'error');
    } finally {
        console.log('\n⏱️ Keeping browser open for 5 seconds...');
        await page.waitForTimeout(5000);
        await browser.close();
    }
    
    return results;
}

// Run the test
console.log('Starting final edit workflow test...\n');
runFinalEditWorkflowTest().then(results => {
    console.log('\n' + '=' .repeat(70));
    console.log('📊 FINAL REPORT - COMPLETE WORKFLOW TEST RESULTS');
    console.log('=' .repeat(70));
    
    console.log('\n✅ Test Results:');
    console.log(`  • Login successful: ${results.loginSuccess ? '✅ YES' : '❌ NO'}`);
    console.log(`  • Events found: ${results.eventsFound}`);
    console.log(`  • Fields populated: ${results.fieldsPopulated}/${results.totalFields}`);
    console.log(`  • Changes attempted: ${results.changesAttempted}`);
    console.log(`  • Changes persisted: ${results.changesPersisted}`);
    
    console.log('\n' + '=' .repeat(70));
    console.log('📌 ANSWER TO YOUR SPECIFIC QUESTION:');
    console.log('"From dashboard, select edit event, verify ALL fields');
    console.log(' are populated, edit fields, verify changes are saved"');
    console.log('-' .repeat(70));
    
    if (results.fieldsPopulated >= 15 && 
        results.changesPersisted === results.changesAttempted && 
        results.changesAttempted > 0) {
        
        console.log('\n✅✅✅ COMPLETE SUCCESS! ✅✅✅');
        console.log(`• ${results.fieldsPopulated} fields ARE populated when editing`);
        console.log(`• ALL ${results.changesPersisted} changes ARE saved and persist`);
        console.log('\n🎉 EVENT EDIT WORKFLOW IS FULLY FUNCTIONAL!');
        console.log('THE TEST HAS PASSED!');
        
    } else if (results.fieldsPopulated > 0 && results.changesPersisted > 0) {
        console.log('\n⚠️ PARTIAL SUCCESS');
        console.log(`• ${results.fieldsPopulated} fields populated (expected 15+)`);
        console.log(`• ${results.changesPersisted}/${results.changesAttempted} changes persisted`);
        
        if (results.fieldsPopulated < 15) {
            console.log('\n⚠️ Issue: Not all expected fields are populating');
        }
        if (results.changesPersisted < results.changesAttempted) {
            console.log('⚠️ Issue: Some changes are not persisting after save');
        }
        
    } else {
        console.log('\n❌ TEST FAILED');
        console.log('Unable to verify edit workflow functionality');
        
        if (!results.loginSuccess) {
            console.log('\n⚠️ Login failed - check credentials');
        }
        if (results.eventsFound === 0) {
            console.log('⚠️ No events found - run seeding script first');
        }
    }
    
    console.log('\n📁 Screenshots: screenshots/final-workflow/');
    console.log('=' .repeat(70));
    
}).catch(error => {
    console.error('Fatal error:', error);
    process.exit(1);
});