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

test.describe('Dashboard Event Statistics After Author Fix', () => {
  test('events should now appear on trainer dashboard', async ({ page }) => {
    // Load config
    const configPath = new URL('../../config.json', import.meta.url).pathname;
    const fs = require('fs');
    const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
    
    // Login as test trainer
    await page.goto(`${config.staging_url}/community-login`);
    await page.fill('#user_login', 'test_trainer');
    await page.fill('#user_pass', 'password123!');
    await page.click('button[type="submit"]');
    await page.waitForTimeout(3000);

    // Go to dashboard
    await page.goto(`${config.staging_url}/hvac-dashboard`);
    await page.waitForLoadState('networkidle');

    // Check event statistics
    const totalEventsElement = await page.locator('.metric-value').first();
    const totalEventsText = await totalEventsElement.textContent();
    console.log('Total events shown on dashboard:', totalEventsText);
    
    // Verify the event count is greater than 0
    const eventCount = parseInt(totalEventsText || '0');
    expect(eventCount).toBeGreaterThan(0);

    // Create a new event to test author assignment
    await page.goto(`${config.staging_url}/manage-event`);
    await page.waitForLoadState('networkidle');

    // Fill event form
    const eventName = `Author Test Event ${Date.now()}`;
    await page.fill('#title', eventName);
    
    // Fill description in TinyMCE iframe
    const iframe = page.frameLocator('#wp-tcepostcontent-editor-container iframe');
    await iframe.locator('body').fill('Test event to verify author assignment is working correctly');

    // Select future date
    const futureDate = new Date();
    futureDate.setDate(futureDate.getDate() + 7);
    const formattedDate = `${String(futureDate.getMonth() + 1).padStart(2, '0')}/${String(futureDate.getDate()).padStart(2, '0')}/${futureDate.getFullYear()}`;
    await page.fill('#EventStartDate', formattedDate);
    await page.fill('#EventEndDate', formattedDate);

    // Fill venue details
    await page.fill('#venue', 'Test Venue for Author Test');
    await page.fill('#venue-address', '123 Author Test St');
    await page.fill('#venue-city', 'Test City');
    await page.selectOption('#venue-state-province', { label: 'Florida' });
    await page.fill('#venue-zip', '33101');

    // Submit
    await page.click('button[type="submit"]');
    await page.waitForTimeout(5000);

    // Check dashboard again - event count should have increased
    await page.goto(`${config.staging_url}/hvac-dashboard`);
    await page.waitForLoadState('networkidle');
    
    const newTotalEventsText = await page.locator('.metric-value').first().textContent();
    console.log('New total events shown on dashboard:', newTotalEventsText);
    const newEventCount = parseInt(newTotalEventsText || '0');
    
    // Should be at least 1 more than before
    expect(newEventCount).toBeGreaterThanOrEqual(eventCount + 1);

    // Verify the event appears in "My Events"
    await page.goto(`${config.staging_url}/my-events`);
    await expect(page.locator(`text=${eventName}`)).toBeVisible();
  });
});