/**
 * Test script to verify announcement pages are created properly
 */

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

async function testCreateAnnouncementPages() {
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext();
    const page = await context.newPage();

    try {
        console.log('🚀 Testing Announcement Pages Creation...');
        
        // Navigate to login page  
        console.log('1. Navigating to login page...');
        await page.goto('https://upskill-staging.measurequick.com/training-login/');
        await page.waitForTimeout(2000);
        
        // Login as test master trainer
        console.log('2. Logging in as master trainer...');
        await page.fill('[name="log"]', 'test_master');
        await page.fill('[name="pwd"]', 'TestMaster123!');
        await page.click('[type="submit"]');
        await page.waitForTimeout(3000);
        
        // Test Master Trainer Announcements page
        console.log('3. Testing master trainer announcements page...');
        await page.goto('https://upskill-staging.measurequick.com/master-trainer/announcements/');
        await page.waitForTimeout(2000);
        
        const announcementsTitle = await page.textContent('h1').catch(() => null);
        console.log('Announcements page title:', announcementsTitle);
        
        if (page.url().includes('404') || announcementsTitle?.includes('not found')) {
            console.log('❌ Master announcements page does not exist');
        } else {
            console.log('✅ Master announcements page exists');
        }
        
        // Test Trainer Resources page
        console.log('4. Testing trainer resources page...');
        await page.goto('https://upskill-staging.measurequick.com/trainer/resources/');
        await page.waitForTimeout(2000);
        
        const resourcesTitle = await page.textContent('h1').catch(() => null);
        console.log('Resources page title:', resourcesTitle);
        
        if (page.url().includes('404') || resourcesTitle?.includes('not found')) {
            console.log('❌ Trainer resources page does not exist');
        } else {
            console.log('✅ Trainer resources page exists');
        }
        
        // Test WordPress admin pages list
        console.log('5. Checking WordPress admin pages list...');
        await page.goto('https://upskill-staging.measurequick.com/wp-admin/edit.php?post_type=page');
        await page.waitForTimeout(3000);
        
        // Search for announcement pages
        const searchBox = await page.$('#post-search-input');
        if (searchBox) {
            await searchBox.fill('announcements');
            await page.click('#search-submit');
            await page.waitForTimeout(2000);
            
            const searchResults = await page.$$('.row-title');
            console.log('Found announcement pages:', searchResults.length);
            
            for (let i = 0; i < searchResults.length; i++) {
                const title = await searchResults[i].textContent();
                console.log(`- Page ${i+1}: ${title}`);
            }
        }
        
        console.log('🎉 Test completed!');
        
    } catch (error) {
        console.error('❌ Test failed:', error.message);
        console.error(error.stack);
    } finally {
        await browser.close();
    }
}

// Run the test
testCreateAnnouncementPages().catch(console.error);