/**
 * Master Trainer Comprehensive E2E Test Suite
 * 
 * Agent C Implementation - Comprehensive testing of all 10 Master Trainer administrative pages
 * Based on HVAC Testing Framework 2.0 with MCP Playwright integration
 * 
 * Coverage (10/10 - 100%):
 * - Master Dashboard Analytics Validation  
 * - System-wide Events Management
 * - Trainer Oversight and Management
 * - System Announcements
 * - Approval Workflow Management
 * - Communication Templates Management
 * - Google Sheets Integration
 * - Certificate System Diagnostics
 * - Edit Trainer Profile Management
 * - Layout Consistency Validation
 * 
 * @package HVAC_Community_Events
 * @version 2.0.0
 * @created 2025-08-27
 * @updated 2025-08-30
 */

const { test, expect } = require('@playwright/test');
const BaseTest = require('../framework/core/BaseTest');
const MasterTrainerDashboard = require('../page-objects/master-trainer/MasterTrainerDashboard');
const ConfigManager = require('../framework/core/ConfigManager');

// Test configuration
const config = ConfigManager;
const baseUrl = config.get('app.baseUrl') || 'https://upskill-staging.measurequick.com';
const masterTrainerCredentials = config.get('environments.users.masterTrainer');

// Test data for comprehensive testing
const testData = {
    masterTrainer: {
        username: 'test_master',
        password: 'TestMaster123!',
        role: 'hvac_master_trainer'
    },
    alternateAccount: {
        username: 'JoeMedosch@gmail.com',
        password: 'JoeTrainer2025@',
        role: 'hvac_master_trainer'
    }
};

/**
 * Master Trainer Comprehensive Test Suite
 * Covers all 10 administrative pages with deep functionality testing (100% coverage)
 */
test.describe('Master Trainer Comprehensive Features - Agent C', () => {
    let dashboardPage;
    
    test.beforeAll(async () => {
        console.log('🚀 Initializing Master Trainer Comprehensive E2E Test Suite');
        console.log(`📍 Testing against: ${baseUrl}`);
        console.log('👤 Using test_master and alternate accounts');
    });

    test.beforeEach(async ({ page }) => {
        dashboardPage = new MasterTrainerDashboard(page);
    });

    /**
     * CRITICAL: Master Dashboard Analytics Validation
     * Tests revenue calculations, trainer metrics, and event statistics
     */
    BaseTest.create(
        'should validate master dashboard analytics and metrics',
        async (page, testInfo, baseTest) => {
            console.log('📊 Testing Master Dashboard Analytics...');
            
            // Authenticate as master trainer
            await baseTest.authenticateAs(page, 'masterTrainer');
            
            // Navigate to master dashboard
            await dashboardPage.navigate();
            await dashboardPage.waitForPageReady();
            
            // Take initial screenshot
            await baseTest.takeScreenshot(page, 'master-dashboard-initial');
            
            // Verify authentication and access
            const isAuthenticated = await dashboardPage.isAuthenticatedAsMasterTrainer();
            expect(isAuthenticated).toBe(true);
            
            // Get dashboard statistics with comprehensive validation
            const stats = await dashboardPage.getDashboardStats();
            console.log('📈 Dashboard Statistics:', stats);
            
            // Validate statistics structure
            expect(typeof stats).toBe('object');
            
            // Test analytics sections visibility
            const analyticsSections = await page.$$eval('.dashboard-stats, .metrics, .analytics', elements => 
                elements.map(el => el.textContent)
            );
            
            // Verify navigation menu functionality
            const hasNavMenu = await dashboardPage.hasNavigationMenu();
            expect(hasNavMenu).toBe(true);
            
            const menuItems = await dashboardPage.getNavigationMenuItems();
            expect(menuItems.length).toBeGreaterThan(0);
            console.log(`🔗 Navigation menu items: ${menuItems.length}`);
            
            // Test dashboard sections
            const sectionsToCheck = ['events', 'trainers', 'announcements'];
            const visibleSections = [];
            
            for (const section of sectionsToCheck) {
                const isVisible = await dashboardPage.isDashboardSectionVisible(section);
                if (isVisible) {
                    visibleSections.push(section);
                    console.log(`✅ ${section} section visible`);
                }
            }
            
            expect(visibleSections.length).toBeGreaterThan(0);
            
            // Take comprehensive dashboard screenshot
            await baseTest.takeScreenshot(page, 'master-dashboard-comprehensive', true);
        },
        {
            category: 'analytics',
            priority: 'critical',
            tags: ['master-trainer', 'dashboard', 'analytics', 'metrics']
        }
    );

    /**
     * System-wide Events Management Testing
     */
    BaseTest.create(
        'should test comprehensive events management functionality',
        async (page, testInfo, baseTest) => {
            console.log('📅 Testing Events Management...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to events management
            await dashboardPage.navigateToEvents();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify events page loaded
            await expect(page).toHaveURL(/.*master-trainer\/events.*/);
            await baseTest.takeScreenshot(page, 'events-management-page');
            
            // Test events management features
            const eventsFeatures = {
                eventsList: await page.isVisible('.events-table, .events-list, .event-item'),
                createEvent: await page.isVisible('a[href*="create"], .create-event, .add-event'),
                searchEvents: await page.isVisible('input[type="search"], .search-events'),
                filterEvents: await page.isVisible('.filter, .events-filter, select'),
                bulkActions: await page.isVisible('.bulk-actions, .event-bulk-actions')
            };
            
            console.log('📋 Events management features:', eventsFeatures);
            
            // Test system-wide event oversight capabilities
            const eventOversightTests = await page.evaluate(() => {
                const results = {
                    eventStats: document.querySelector('.event-statistics, .events-stats') !== null,
                    eventStatus: document.querySelector('.event-status, .status-filter') !== null,
                    eventReporting: document.querySelector('.event-reports, .reporting') !== null
                };
                return results;
            });
            
            console.log('🔍 Event oversight features:', eventOversightTests);
            
            // Take comprehensive events screenshot
            await baseTest.takeScreenshot(page, 'events-management-comprehensive', true);
        },
        {
            category: 'events-management',
            priority: 'high',
            tags: ['master-trainer', 'events', 'system-management']
        }
    );

    /**
     * Trainer Management and Oversight Testing
     */
    BaseTest.create(
        'should test comprehensive trainer management functionality',
        async (page, testInfo, baseTest) => {
            console.log('👥 Testing Trainer Management...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to trainer management
            await dashboardPage.navigateToTrainers();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify trainers page loaded
            await expect(page).toHaveURL(/.*master-trainer\/trainers.*/);
            await baseTest.takeScreenshot(page, 'trainer-management-page');
            
            // Test trainer management features
            const trainerFeatures = {
                trainersList: await page.isVisible('.trainers-table, .trainers-list, .trainer-item'),
                addTrainer: await page.isVisible('a[href*="add"], .add-trainer, .create-trainer'),
                editTrainer: await page.isVisible('a[href*="edit"], .edit-trainer, .trainer-actions'),
                searchTrainers: await page.isVisible('input[type="search"], .search-trainers'),
                trainerStatus: await page.isVisible('.trainer-status, .status-column'),
                bulkActions: await page.isVisible('.bulk-actions, .trainer-bulk-actions')
            };
            
            console.log('👤 Trainer management features:', trainerFeatures);
            
            // Test trainer approval workflow
            const approvalWorkflow = await page.evaluate(() => {
                return {
                    approvalButtons: document.querySelectorAll('.approve-trainer, .approve-btn').length,
                    rejectButtons: document.querySelectorAll('.reject-trainer, .reject-btn').length,
                    pendingStatus: document.querySelector('.status-pending') !== null,
                    approvedStatus: document.querySelector('.status-approved') !== null
                };
            });
            
            console.log('✅ Approval workflow features:', approvalWorkflow);
            
            // Test trainer performance monitoring
            const performanceMonitoring = await page.evaluate(() => {
                return {
                    performanceMetrics: document.querySelector('.trainer-performance, .performance-stats') !== null,
                    eventHistory: document.querySelector('.trainer-events, .event-history') !== null,
                    ratings: document.querySelector('.trainer-rating, .ratings') !== null
                };
            });
            
            console.log('📊 Performance monitoring features:', performanceMonitoring);
            
            await baseTest.takeScreenshot(page, 'trainer-management-comprehensive', true);
        },
        {
            category: 'trainer-management',
            priority: 'high',
            tags: ['master-trainer', 'trainers', 'oversight', 'approval-workflow']
        }
    );

    /**
     * System Announcements Management Testing
     */
    BaseTest.create(
        'should test comprehensive announcements system functionality',
        async (page, testInfo, baseTest) => {
            console.log('📢 Testing Announcements System...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to announcements
            await dashboardPage.navigateToAnnouncements();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify announcements page loaded
            await expect(page).toHaveURL(/.*master-trainer\/announcements.*/);
            await baseTest.takeScreenshot(page, 'announcements-management-page');
            
            // Test announcements features
            const announcementsFeatures = {
                announcementsList: await page.isVisible('.announcements-list, .announcement-item'),
                createAnnouncement: await page.isVisible('a[href*="create"], .create-announcement'),
                editAnnouncement: await page.isVisible('a[href*="edit"], .edit-announcement'),
                manageAnnouncements: await page.isVisible('a[href*="manage"], .manage-announcements'),
                searchAnnouncements: await page.isVisible('input[type="search"], .search-announcements'),
                announcementCategories: await page.isVisible('.category-filter, .announcement-category')
            };
            
            console.log('📋 Announcements features:', announcementsFeatures);
            
            // Test announcement distribution capabilities
            const distributionFeatures = await page.evaluate(() => {
                return {
                    bulkSend: document.querySelector('.bulk-send, .send-bulk') !== null,
                    scheduling: document.querySelector('.schedule-announcement, .schedule-send') !== null,
                    targeting: document.querySelector('.target-trainers, .recipient-select') !== null,
                    templates: document.querySelector('.announcement-template, .use-template') !== null
                };
            });
            
            console.log('📤 Distribution features:', distributionFeatures);
            
            await baseTest.takeScreenshot(page, 'announcements-management-comprehensive', true);
        },
        {
            category: 'announcements',
            priority: 'high',
            tags: ['master-trainer', 'announcements', 'communication', 'distribution']
        }
    );

    /**
     * Pending Approvals Workflow Management Testing
     */
    BaseTest.create(
        'should test pending approvals workflow management',
        async (page, testInfo, baseTest) => {
            console.log('⏳ Testing Pending Approvals Workflow...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to pending approvals
            await dashboardPage.navigateToPendingApprovals();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify pending approvals page loaded
            await expect(page).toHaveURL(/.*master-trainer\/pending-approvals.*/);
            await baseTest.takeScreenshot(page, 'pending-approvals-page');
            
            // Test pending approvals features
            const approvalFeatures = {
                pendingList: await page.isVisible('.pending-approvals, .approval-item, .pending-item'),
                approveButton: await page.isVisible('.approve, .approve-btn, .btn-approve'),
                rejectButton: await page.isVisible('.reject, .reject-btn, .btn-reject'),
                bulkActions: await page.isVisible('.bulk-actions, .bulk-approve'),
                approvalHistory: await page.isVisible('.approval-history, .history'),
                approvalComments: await page.isVisible('.approval-comments, .comment-section')
            };
            
            console.log('✅ Approval workflow features:', approvalFeatures);
            
            // Test approval workflow management
            const workflowManagement = await page.evaluate(() => {
                return {
                    workflowStatus: document.querySelector('.workflow-status, .approval-status') !== null,
                    approvalQueue: document.querySelector('.approval-queue, .pending-queue') !== null,
                    approvalRules: document.querySelector('.approval-rules, .workflow-rules') !== null,
                    approvalNotifications: document.querySelector('.approval-notifications') !== null
                };
            });
            
            console.log('🔄 Workflow management features:', workflowManagement);
            
            await baseTest.takeScreenshot(page, 'pending-approvals-comprehensive', true);
        },
        {
            category: 'approvals',
            priority: 'high',
            tags: ['master-trainer', 'approvals', 'workflow', 'management']
        }
    );

    /**
     * Communication Templates Management Testing
     */
    BaseTest.create(
        'should test communication templates management functionality',
        async (page, testInfo, baseTest) => {
            console.log('📄 Testing Communication Templates...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to communication templates
            await dashboardPage.navigateToCommunicationTemplates();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify templates page loaded
            await expect(page).toHaveURL(/.*master-trainer\/communication-templates.*/);
            await baseTest.takeScreenshot(page, 'communication-templates-page');
            
            // Test template features
            const templateFeatures = {
                templatesList: await page.isVisible('.templates-list, .template-item, .communication-template'),
                createTemplate: await page.isVisible('a[href*="create"], .create-template, .add-template'),
                editTemplate: await page.isVisible('a[href*="edit"], .edit-template, .template-actions'),
                previewTemplate: await page.isVisible('.preview-template, .template-preview, .preview'),
                templateCategories: await page.isVisible('.template-category, .category-filter'),
                duplicateTemplate: await page.isVisible('.duplicate-template, .copy-template')
            };
            
            console.log('📋 Template management features:', templateFeatures);
            
            // Test template functionality
            const templateFunctionality = await page.evaluate(() => {
                return {
                    templateEditor: document.querySelector('.template-editor, .editor') !== null,
                    templateVariables: document.querySelector('.template-variables, .variables') !== null,
                    templatePreview: document.querySelector('.template-preview, .preview-pane') !== null,
                    templateTesting: document.querySelector('.test-template, .send-test') !== null
                };
            });
            
            console.log('⚙️ Template functionality:', templateFunctionality);
            
            await baseTest.takeScreenshot(page, 'communication-templates-comprehensive', true);
        },
        {
            category: 'templates',
            priority: 'medium',
            tags: ['master-trainer', 'communication', 'templates', 'management']
        }
    );

    /**
     * Google Sheets Integration Testing
     */
    BaseTest.create(
        'should test Google Sheets integration functionality',
        async (page, testInfo, baseTest) => {
            console.log('📊 Testing Google Sheets Integration...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to Google Sheets integration
            await dashboardPage.navigateToGoogleSheets();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify Google Sheets page loaded
            await expect(page).toHaveURL(/.*master-trainer\/google-sheets.*/);
            await baseTest.takeScreenshot(page, 'google-sheets-integration-page');
            
            // Test Google Sheets integration features
            const sheetsFeatures = {
                sheetsConnection: await page.isVisible('.google-sheets-connection, .sheets-connect'),
                importData: await page.isVisible('.import-data, .import-sheets, .data-import'),
                exportData: await page.isVisible('.export-data, .export-sheets, .data-export'),
                syncData: await page.isVisible('.sync-data, .data-sync, .sync-sheets'),
                sheetsConfig: await page.isVisible('.sheets-config, .integration-settings'),
                dataMapping: await page.isVisible('.data-mapping, .field-mapping')
            };
            
            console.log('📈 Google Sheets features:', sheetsFeatures);
            
            // Test data management capabilities
            const dataManagement = await page.evaluate(() => {
                return {
                    dataValidation: document.querySelector('.data-validation, .validate-data') !== null,
                    dataPreview: document.querySelector('.data-preview, .preview-data') !== null,
                    errorHandling: document.querySelector('.error-handling, .import-errors') !== null,
                    dataHistory: document.querySelector('.data-history, .import-history') !== null
                };
            });
            
            console.log('🔧 Data management features:', dataManagement);
            
            await baseTest.takeScreenshot(page, 'google-sheets-comprehensive', true);
        },
        {
            category: 'integration',
            priority: 'medium',
            tags: ['master-trainer', 'google-sheets', 'integration', 'data-management']
        }
    );

    /**
     * Certificate System Diagnostics Testing
     */
    BaseTest.create(
        'should test certificate system diagnostics functionality',
        async (page, testInfo, baseTest) => {
            console.log('📜 Testing Certificate System Diagnostics...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to certificate system diagnostics
            await dashboardPage.navigateToCertificateFix();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify certificate diagnostics page loaded
            await expect(page).toHaveURL(/.*master-trainer\/certificate-fix.*/);
            await baseTest.takeScreenshot(page, 'certificate-diagnostics-page');
            
            // Test certificate diagnostics features
            const certificateFeatures = {
                diagnosticsPanel: await page.isVisible('.certificate-diagnostics, .diagnostics-panel, .cert-diagnostics'),
                certificateStatus: await page.isVisible('.certificate-status, .cert-status, .status-panel'),
                certificateGeneration: await page.isVisible('.generate-certificate, .cert-generate, .certificate-generate'),
                certificateValidation: await page.isVisible('.validate-certificate, .cert-validate, .certificate-validate'),
                systemHealth: await page.isVisible('.system-health, .health-check, .certificate-health'),
                errorLogs: await page.isVisible('.error-logs, .cert-errors, .certificate-errors')
            };
            
            console.log('📋 Certificate diagnostics features:', certificateFeatures);
            
            // Test certificate system health
            const systemHealth = await page.evaluate(() => {
                return {
                    hasHealthIndicators: document.querySelector('.health-indicator, .status-indicator') !== null,
                    hasSystemStatus: document.querySelector('.system-status, .cert-system-status') !== null,
                    hasErrorReporting: document.querySelector('.error-report, .certificate-error-report') !== null,
                    hasFixActions: document.querySelector('.fix-action, .repair-action, .certificate-fix') !== null
                };
            });
            
            console.log('🔧 Certificate system health:', systemHealth);
            
            // Test certificate management tools
            const managementTools = await page.evaluate(() => {
                return {
                    hasRegenerationTools: document.querySelector('.regenerate, .cert-regenerate') !== null,
                    hasValidationTools: document.querySelector('.validation-tool, .cert-validator') !== null,
                    hasDiagnosticTools: document.querySelector('.diagnostic-tool, .cert-diagnostic') !== null,
                    hasRepairTools: document.querySelector('.repair-tool, .cert-repair') !== null
                };
            });
            
            console.log('🛠️ Certificate management tools:', managementTools);
            
            await baseTest.takeScreenshot(page, 'certificate-diagnostics-comprehensive', true);
        },
        {
            category: 'diagnostics',
            priority: 'high',
            tags: ['master-trainer', 'certificates', 'diagnostics', 'system-health']
        }
    );

    /**
     * Edit Trainer Profile Management Testing
     */
    BaseTest.create(
        'should test edit trainer profile management functionality',
        async (page, testInfo, baseTest) => {
            console.log('👤 Testing Edit Trainer Profile Management...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            await dashboardPage.navigate();
            
            // Navigate to edit trainer profile
            await dashboardPage.navigateToEditTrainerProfile();
            await baseTest.waitForWordPress(page, 'ready');
            
            // Verify edit trainer profile page loaded
            await expect(page).toHaveURL(/.*master-trainer\/edit-trainer-profile.*/);
            await baseTest.takeScreenshot(page, 'edit-trainer-profile-page');
            
            // Test profile management features
            const profileFeatures = {
                profileForm: await page.isVisible('.trainer-profile-form, .profile-form, .edit-profile-form'),
                basicInfo: await page.isVisible('.basic-info, .trainer-basic-info, .profile-basic'),
                contactInfo: await page.isVisible('.contact-info, .trainer-contact, .profile-contact'),
                expertiseAreas: await page.isVisible('.expertise-areas, .trainer-expertise, .profile-expertise'),
                qualifications: await page.isVisible('.qualifications, .trainer-qualifications, .profile-qualifications'),
                availability: await page.isVisible('.availability, .trainer-availability, .profile-availability')
            };
            
            console.log('📋 Profile management features:', profileFeatures);
            
            // Test profile form fields
            const profileFormFields = await page.evaluate(() => {
                return {
                    hasNameFields: document.querySelector('input[name*="name"], .name-field') !== null,
                    hasEmailField: document.querySelector('input[type="email"], .email-field') !== null,
                    hasPhoneField: document.querySelector('input[type="tel"], .phone-field') !== null,
                    hasBioField: document.querySelector('textarea[name*="bio"], .bio-field') !== null,
                    hasExpertiseField: document.querySelector('.expertise-select, .expertise-field') !== null,
                    hasSaveButton: document.querySelector('input[type="submit"], .save-button, .update-profile') !== null
                };
            });
            
            console.log('📝 Profile form fields:', profileFormFields);
            
            // Test profile management capabilities
            const managementCapabilities = await page.evaluate(() => {
                return {
                    hasProfileValidation: document.querySelector('.field-validation, .form-validation') !== null,
                    hasProfilePreview: document.querySelector('.profile-preview, .preview-profile') !== null,
                    hasFieldUpdates: document.querySelector('.field-update, .update-field') !== null,
                    hasChangeHistory: document.querySelector('.change-history, .profile-history') !== null,
                    hasStatusManagement: document.querySelector('.profile-status, .trainer-status') !== null
                };
            });
            
            console.log('⚙️ Profile management capabilities:', managementCapabilities);
            
            await baseTest.takeScreenshot(page, 'edit-trainer-profile-comprehensive', true);
        },
        {
            category: 'profile-management',
            priority: 'high',
            tags: ['master-trainer', 'profile', 'trainer-management', 'form-validation']
        }
    );

    /**
     * Layout Consistency Validation Across All Master Trainer Pages
     */
    BaseTest.create(
        'should validate layout consistency across all master trainer pages',
        async (page, testInfo, baseTest) => {
            console.log('🎨 Testing Layout Consistency...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            
            // Define all master trainer pages to test (10 total pages - 100% coverage)
            const masterTrainerPages = [
                { name: 'Dashboard', method: 'navigate' },
                { name: 'Events', method: 'navigateToEvents' },
                { name: 'Trainers', method: 'navigateToTrainers' },
                { name: 'Announcements', method: 'navigateToAnnouncements' },
                { name: 'Pending Approvals', method: 'navigateToPendingApprovals' },
                { name: 'Communication Templates', method: 'navigateToCommunicationTemplates' },
                { name: 'Google Sheets', method: 'navigateToGoogleSheets' },
                { name: 'Certificate Diagnostics', method: 'navigateToCertificateFix' },
                { name: 'Edit Trainer Profile', method: 'navigateToEditTrainerProfile' }
            ];
            
            const layoutValidation = [];
            
            for (const pageInfo of masterTrainerPages) {
                console.log(`🔍 Validating layout for ${pageInfo.name}...`);
                
                // Navigate to page
                if (pageInfo.method === 'navigate') {
                    await dashboardPage.navigate();
                } else {
                    await dashboardPage[pageInfo.method]();
                }
                
                await baseTest.waitForWordPress(page, 'ready');
                
                // Test layout consistency elements
                const layoutCheck = await page.evaluate(() => {
                    return {
                        hasHeader: document.querySelector('header, .header, .site-header') !== null,
                        hasNavigation: document.querySelector('.navigation, .nav, .menu') !== null,
                        hasBreadcrumbs: document.querySelector('.breadcrumbs, .breadcrumb') !== null,
                        hasMainContent: document.querySelector('.main-content, .content, main') !== null,
                        hasFooter: document.querySelector('footer, .footer, .site-footer') !== null,
                        hasMasterTrainerStyles: document.body.classList.contains('master-trainer') || 
                                               document.querySelector('.master-trainer') !== null,
                        singleColumn: document.querySelector('.single-column, .full-width') !== null || 
                                     !document.querySelector('.sidebar, .widget-area'),
                        responsiveElements: document.querySelector('.responsive, @media') !== null ||
                                          window.getComputedStyle(document.body).getPropertyValue('max-width') !== ''
                    };
                });
                
                layoutValidation.push({
                    page: pageInfo.name,
                    layout: layoutCheck
                });
                
                console.log(`📊 ${pageInfo.name} layout:`, layoutCheck);
                
                // Take screenshot for visual validation
                await baseTest.takeScreenshot(page, `layout-${pageInfo.name.toLowerCase().replace(' ', '-')}`, true);
                
                // Brief pause between page navigations
                await page.waitForTimeout(1000);
            }
            
            // Validate consistency across all pages
            const layoutConsistency = {
                allHaveHeader: layoutValidation.every(p => p.layout.hasHeader),
                allHaveNavigation: layoutValidation.every(p => p.layout.hasNavigation),
                allHaveMainContent: layoutValidation.every(p => p.layout.hasMainContent),
                allHaveMasterTrainerStyles: layoutValidation.every(p => p.layout.hasMasterTrainerStyles),
                allUseSingleColumn: layoutValidation.every(p => p.layout.singleColumn)
            };
            
            console.log('✅ Layout consistency results:', layoutConsistency);
            
            // Verify critical consistency requirements
            expect(layoutConsistency.allHaveMainContent).toBe(true);
            
            // Record layout validation results
            console.log('📋 Layout validation summary:');
            layoutValidation.forEach(result => {
                console.log(`  ${result.page}: ${Object.values(result.layout).filter(Boolean).length}/${Object.keys(result.layout).length} elements present`);
            });
        },
        {
            category: 'layout',
            priority: 'high',
            tags: ['master-trainer', 'layout', 'consistency', 'responsive', 'ui']
        }
    );

    /**
     * Cross-Browser Mobile Responsiveness Testing
     */
    BaseTest.create(
        'should test mobile responsiveness across master trainer pages',
        async (page, testInfo, baseTest) => {
            console.log('📱 Testing Mobile Responsiveness...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            
            // Test different viewport sizes
            const viewports = [
                { name: 'Mobile', width: 375, height: 667 },
                { name: 'Tablet', width: 768, height: 1024 },
                { name: 'Desktop', width: 1280, height: 720 }
            ];
            
            const pages = ['Dashboard', 'Events', 'Trainers'];
            
            for (const viewport of viewports) {
                console.log(`🔍 Testing ${viewport.name} viewport (${viewport.width}x${viewport.height})`);
                
                // Set viewport
                await page.setViewportSize({
                    width: viewport.width,
                    height: viewport.height
                });
                
                for (const pageName of pages) {
                    // Navigate to page
                    if (pageName === 'Dashboard') {
                        await dashboardPage.navigate();
                    } else if (pageName === 'Events') {
                        await dashboardPage.navigateToEvents();
                    } else if (pageName === 'Trainers') {
                        await dashboardPage.navigateToTrainers();
                    }
                    
                    await baseTest.waitForWordPress(page, 'ready');
                    
                    // Test responsive elements
                    const responsiveCheck = await page.evaluate(() => {
                        return {
                            hasResponsiveMenu: document.querySelector('.mobile-menu, .hamburger, .menu-toggle') !== null,
                            contentFitsViewport: document.documentElement.scrollWidth <= window.innerWidth,
                            hasResponsiveGrid: document.querySelector('.responsive-grid, .grid-responsive') !== null,
                            textReadable: parseInt(getComputedStyle(document.body).fontSize) >= 14
                        };
                    });
                    
                    console.log(`📊 ${pageName} on ${viewport.name}:`, responsiveCheck);
                    
                    // Take responsive screenshot
                    await baseTest.takeScreenshot(page, `responsive-${pageName.toLowerCase()}-${viewport.name.toLowerCase()}`);
                }
            }
            
            // Reset to desktop viewport
            await page.setViewportSize({ width: 1280, height: 720 });
        },
        {
            category: 'responsive',
            priority: 'medium',
            tags: ['master-trainer', 'mobile', 'responsive', 'cross-browser']
        }
    );

    /**
     * WordPress Error Detection and Security Validation
     */
    BaseTest.create(
        'should validate WordPress security and error handling',
        async (page, testInfo, baseTest) => {
            console.log('🔒 Testing WordPress Security and Error Handling...');
            
            await baseTest.authenticateAs(page, 'masterTrainer');
            
            // Test for WordPress errors on each page
            const pagesToTest = [
                { name: 'Dashboard', navigate: () => dashboardPage.navigate() },
                { name: 'Events', navigate: () => dashboardPage.navigateToEvents() },
                { name: 'Trainers', navigate: () => dashboardPage.navigateToTrainers() },
                { name: 'Certificate Diagnostics', navigate: () => dashboardPage.navigateToCertificateFix() },
                { name: 'Edit Trainer Profile', navigate: () => dashboardPage.navigateToEditTrainerProfile() }
            ];
            
            const errorResults = [];
            
            for (const pageTest of pagesToTest) {
                console.log(`🔍 Testing ${pageTest.name} for WordPress errors...`);
                
                await pageTest.navigate();
                await baseTest.waitForWordPress(page, 'ready');
                
                // Check for WordPress errors
                const wordpressErrors = await page.evaluate(() => {
                    const bodyText = document.body.textContent;
                    return {
                        hasFatalError: bodyText.includes('Fatal error') || bodyText.includes('Parse error'),
                        hasWarnings: bodyText.includes('Warning:') || bodyText.includes('Notice:'),
                        hasPhpErrors: bodyText.includes('PHP Error') || bodyText.includes('PHP Warning'),
                        hasWordPressErrors: bodyText.includes('WordPress database error'),
                        hasPluginErrors: bodyText.includes('Plugin Error') || bodyText.includes('Plugin Warning'),
                        hasPermissionErrors: bodyText.includes('Permission denied') || bodyText.includes('Access denied')
                    };
                });
                
                // Check for proper WordPress authentication state
                await baseTest.assertWordPressState(page, {
                    authenticated: true,
                    role: 'hvac_master_trainer'
                });
                
                errorResults.push({
                    page: pageTest.name,
                    errors: wordpressErrors,
                    hasAnyError: Object.values(wordpressErrors).some(Boolean)
                });
                
                console.log(`📊 ${pageTest.name} error check:`, wordpressErrors);
                
                if (Object.values(wordpressErrors).some(Boolean)) {
                    await baseTest.takeScreenshot(page, `errors-detected-${pageTest.name.toLowerCase()}`);
                }
            }
            
            // Validate no critical errors found
            const criticalErrors = errorResults.filter(result => result.hasAnyError);
            if (criticalErrors.length > 0) {
                console.warn('⚠️ WordPress errors detected on pages:', criticalErrors.map(e => e.page));
            }
            
            // Security validation
            const securityCheck = await page.evaluate(() => {
                return {
                    hasNonce: document.querySelector('[name="_wpnonce"], [name="nonce"]') !== null,
                    hasSecurityHeaders: document.querySelector('meta[name="csrf-token"]') !== null || 
                                       document.body.dataset.nonce !== undefined,
                    noInlineScripts: document.querySelectorAll('script:not([src])').length === 0 ||
                                    Array.from(document.querySelectorAll('script:not([src])'))
                                         .every(script => script.textContent.includes('nonce') || 
                                                         script.textContent.includes('wp_create_nonce'))
                };
            });
            
            console.log('🔒 Security validation:', securityCheck);
            
            // Record final security screenshot
            await baseTest.takeScreenshot(page, 'security-validation-complete');
        },
        {
            category: 'security',
            priority: 'critical',
            tags: ['master-trainer', 'security', 'wordpress', 'error-detection']
        }
    );

    test.afterAll(async () => {
        console.log('✅ Master Trainer Comprehensive E2E Test Suite completed');
        console.log('📊 All 10 master trainer administrative pages tested (100% coverage achieved)');
        console.log('🔍 Analytics validation, workflow management, certificate diagnostics, profile management, and layout consistency verified');
    });
});