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

async function testDockerEnvironment() {
    console.log('🚀 Testing Docker WordPress environment...');
    
    const baseUrl = 'http://localhost:8080';
    console.log('📍 Base URL:', baseUrl);
    
    const browser = await chromium.launch({ headless: true });
    const page = await browser.newPage();
    
    try {
        console.log('🌐 Navigating to WordPress site...');
        await page.goto(baseUrl, { timeout: 15000 });
        
        const title = await page.title();
        console.log('📄 Page title:', title);
        
        // Check if it's a WordPress site
        const isWordPress = await page.evaluate(() => {
            return !!document.querySelector('meta[name="generator"][content*="WordPress"]') ||
                   !!document.querySelector('link[href*="wp-content"]') ||
                   typeof window.wp !== 'undefined';
        });
        
        console.log('🏥 WordPress detected:', isWordPress);
        
        // Check for HVAC plugin signs
        const hasHVAC = await page.evaluate(() => {
            return document.body.innerHTML.includes('hvac') ||
                   document.body.innerHTML.includes('trainer') ||
                   document.querySelector('[class*="hvac"]') !== null ||
                   document.querySelector('[id*="hvac"]') !== null;
        });
        
        console.log('🔧 HVAC plugin signs:', hasHVAC);
        
        // Take screenshot for inspection
        await page.screenshot({ path: 'test-evidence/docker-homepage.png', fullPage: true });
        console.log('📸 Homepage screenshot: test-evidence/docker-homepage.png');
        
        // Try to access login page
        const loginUrl = baseUrl + '/wp-login.php';
        await page.goto(loginUrl, { timeout: 10000 });
        const loginTitle = await page.title();
        console.log('🔐 Login page title:', loginTitle);
        
        await page.screenshot({ path: 'test-evidence/docker-login.png', fullPage: true });
        console.log('📸 Login screenshot: test-evidence/docker-login.png');
        
        // Try community login
        try {
            const communityLoginUrl = baseUrl + '/training-login/';
            await page.goto(communityLoginUrl, { timeout: 10000 });
            const communityTitle = await page.title();
            console.log('👥 Community login title:', communityTitle);
            
            await page.screenshot({ path: 'test-evidence/docker-community-login.png', fullPage: true });
            console.log('📸 Community login screenshot: test-evidence/docker-community-login.png');
        } catch (error) {
            console.log('⚠️ Community login page not available:', error.message);
        }
        
        console.log('✅ Docker environment test completed successfully');
        return true;
        
    } catch (error) {
        console.error('❌ Docker environment test failed:', error.message);
        await page.screenshot({ path: 'test-evidence/docker-error.png', fullPage: true });
        console.log('📸 Error screenshot: test-evidence/docker-error.png');
        return false;
    } finally {
        await browser.close();
    }
}

testDockerEnvironment()
    .then(success => {
        process.exit(success ? 0 : 1);
    })
    .catch(error => {
        console.error('❌ Unexpected error:', error);
        process.exit(1);
    });