import { test, expect } from '@playwright/test';
import { LoginPage } from './LoginPage';
import { DashboardPage } from './DashboardPage';
import { CreateEventPage } from './CreateEventPage';
import { TEST_USERS } from '../data/test-users';
import { TEST_EVENTS } from '../data/test-events';

test.describe('Working Create Event Test', () => {
    test('Create event with description in text mode', async ({ page }) => {
        const loginPage = new LoginPage(page);
        const dashboardPage = new DashboardPage(page);
        const createEventPage = new CreateEventPage(page);
        const trainer = TEST_USERS.trainer;
        
        // Login
        await loginPage.navigateToLogin();
        await loginPage.login(trainer.username, trainer.password);
        
        // Navigate to create event
        await dashboardPage.clickCreateEvent();
        
        // Wait for form to load
        await page.waitForLoadState('networkidle');
        await page.waitForTimeout(2000);
        
        // Fill event title
        await page.fill('input[name="post_title"]', 'HVAC Fundamentals Training');
        
        // Switch to text mode for description
        const textTab = page.locator('#content-html, a#content-html, button:has-text("Text")');
        if (await textTab.isVisible()) {
            await textTab.click();
            await page.waitForTimeout(1000);
        }
        
        // Now fill the textarea directly
        const textarea = page.locator('textarea#content, textarea.wp-editor-area');
        await textarea.fill('Learn the basics of HVAC systems in this comprehensive training session.');
        
        // Fill dates
        await page.fill('#EventStartDate', '6/1/2025');
        await page.fill('#EventEndDate', '6/1/2025');
        
        // Fill times
        await page.fill('#EventStartTime', '09:00');
        await page.fill('#EventEndTime', '17:00');
        
        // Submit the form
        await page.click('input[value="Submit Event"]');
        
        // Wait for response
        await page.waitForLoadState('networkidle');
        await page.waitForTimeout(3000);
        
        console.log(`Final URL: ${page.url()}`);
        
        // Take screenshot
        await page.screenshot({ path: 'working-create-event-result.png', fullPage: true });
        
        // Check for success
        const successMessage = page.locator('.tribe-community-notice, .updated, .success-message, .notice-success, h1:has-text("Event Submitted"), h1:has-text("Event Created")');
        const errorMessage = page.locator('.tribe-community-notice-error, .error, .notice-error');
        
        if (await successMessage.isVisible()) {
            const message = await successMessage.textContent();
            console.log(`Success: ${message}`);
        }
        
        if (await errorMessage.isVisible()) {
            const message = await errorMessage.textContent();
            console.log(`Error: ${message}`);
        }
        
        // Check if we're redirected to a success page or event view
        if (page.url().includes('event') || page.url().includes('submitted') || page.url().includes('success')) {
            console.log('Event created successfully!');
        }
    });
});