import { test, expect } from '@playwright/test';

test('Test Dashboard with Admin User', async ({ page }) => {
    console.log('Testing with admin credentials...');
    
    // Go to WordPress admin login
    await page.goto('https://upskill-staging.measurequick.com/wp-admin/');
    await page.waitForLoadState('networkidle');
    
    // Try admin login
    await page.fill('#user_login', 'devadmin');
    await page.fill('#user_pass', 'zc3CQlCOr9ZZBZDakecTqcOt');
    await page.click('#wp-submit');
    
    await page.waitForLoadState('networkidle');
    console.log('Admin login URL:', page.url());
    
    if (page.url().includes('wp-admin')) {
        console.log('✓ Admin login successful');
        
        // Navigate to HVAC dashboard
        await page.goto('https://upskill-staging.measurequick.com/hvac-dashboard/');
        await page.waitForLoadState('networkidle');
        
        console.log('Dashboard URL:', page.url());
        console.log('Dashboard Title:', await page.title());
        
        // Take screenshot
        await page.screenshot({ path: 'admin-dashboard-test.png' });
        
        // Check for dashboard elements
        const statsCards = await page.locator('.hvac-stat-card').count();
        console.log('Stats cards found:', statsCards);
        
        if (statsCards > 0) {
            console.log('✓ Dashboard stats are visible');
            
            // Get stat values
            const totalEventsElement = page.locator('.hvac-stat-card:has-text("Total Events") .hvac-stat-value');
            const upcomingEventsElement = page.locator('.hvac-stat-card:has-text("Upcoming Events") .hvac-stat-value');
            const ticketsSoldElement = page.locator('.hvac-stat-card:has-text("Tickets Sold") .hvac-stat-value');
            const totalRevenueElement = page.locator('.hvac-stat-card:has-text("Total Revenue") .hvac-stat-value');
            
            const totalEvents = await totalEventsElement.textContent().catch(() => 'N/A');
            const upcomingEvents = await upcomingEventsElement.textContent().catch(() => 'N/A');
            const ticketsSold = await ticketsSoldElement.textContent().catch(() => 'N/A');
            const totalRevenue = await totalRevenueElement.textContent().catch(() => 'N/A');
            
            console.log('Dashboard Stats:');
            console.log('- Total Events:', totalEvents);
            console.log('- Upcoming Events:', upcomingEvents);
            console.log('- Tickets Sold:', ticketsSold);
            console.log('- Total Revenue:', totalRevenue);
        }
        
        // Check certificate reports
        await page.goto('https://upskill-staging.measurequick.com/certificate-reports/');
        await page.waitForLoadState('networkidle');
        
        console.log('Certificate Reports URL:', page.url());
        
        const hasError = await page.locator('text=critical error').count() > 0;
        if (hasError) {
            console.log('✗ Certificate Reports still has critical error');
        } else {
            console.log('✓ Certificate Reports loads without critical error');
        }
        
        await page.screenshot({ path: 'certificate-reports-test.png' });
        
    } else {
        console.log('✗ Admin login failed');
        await page.screenshot({ path: 'admin-login-failed.png' });
    }
});