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

test.describe('Debug Community Login Error Messages', () => {
    test('Find error message after invalid login', async ({ page }) => {
        // Navigate to community login page
        await page.goto('/community-login/');
        
        // Fill invalid credentials
        await page.fill('#user_login', 'invalid@example.com');
        await page.fill('#user_pass', 'wrongpassword');
        
        // Click login button
        await page.click('#wp-submit');
        
        // Wait for error to appear or page to reload
        await page.waitForTimeout(3000);
        
        // Find all visible error messages
        const errorSelectors = [
            '#login_error',
            '.login-error',
            '.message',
            '.error',
            '.notice-error',
            'form .error-message',
            '[role="alert"]',
            '.woocommerce-error',
            '.woocommerce-message',
            '.notice',
            '.alert',
            'p.error',
            '.login .message'
        ];
        
        console.log(`Current URL: ${page.url()}`);
        
        for (const selector of errorSelectors) {
            const elements = await page.locator(selector).all();
            for (const element of elements) {
                if (await element.isVisible()) {
                    const text = await element.textContent();
                    console.log(`Found error with selector '${selector}': "${text?.trim()}"`);
                }
            }
        }
        
        // Also check the entire page for any text containing 'error' or 'invalid'
        const bodyText = await page.locator('body').textContent();
        const errorMatches = bodyText?.match(/.*error.*|.*invalid.*|.*incorrect.*|.*failed.*/gi);
        if (errorMatches) {
            console.log('Text containing error/invalid/incorrect/failed:', 
                errorMatches.filter(m => m.length < 200).map(m => m.trim()));
        }
        
        // Take a screenshot for visual inspection
        await page.screenshot({ path: 'community-login-error-debug.png', fullPage: true });
        console.log('Screenshot saved to community-login-error-debug.png');
    });
});