import { test, expect } from '@playwright/test';
import { BaseHelpers } from './helpers/base-helpers';
import { LoginPage } from './pages/login-page';
import { DashboardPage } from './pages/dashboard-page';
import { CreateEventPage } from './pages/create-event-page';

test.describe('Dashboard Event Statistics', () => {
  const config = BaseHelpers.loadConfig();
  let loginPage: LoginPage;
  let dashboardPage: DashboardPage;
  let createEventPage: CreateEventPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    dashboardPage = new DashboardPage(page);
    createEventPage = new CreateEventPage(page);
  });

  test('should show events on dashboard after creation', async ({ page }) => {
    // Login as test trainer
    await loginPage.goto();
    await loginPage.login('test_trainer', 'password123!');

    // Go to Create Event page
    await page.goto(`${config.staging_url}/manage-event`);
    await page.waitForLoadState('networkidle');

    // Create a test event with unique name
    const eventName = `Dashboard Test Event ${Date.now()}`;
    await createEventPage.fillEventTitle(eventName);
    await createEventPage.fillEventDescription('Test event to verify dashboard shows events correctly');
    await createEventPage.selectEventDate(); // Uses default date
    await createEventPage.fillEventVenue({
      venueName: 'Test Venue',
      address1: '123 Test St',
      city: 'Test City',
      state: 'Florida',
      zip: '33101'
    });

    // Submit the event
    await createEventPage.submitEvent();
    
    // Wait for submission to complete
    await page.waitForTimeout(3000);

    // Go to dashboard and check if event count has increased
    await dashboardPage.goto();
    await page.waitForLoadState('networkidle');

    // Check if the event stats show at least 1 event
    const totalEventsText = await page.locator('.metric-value').first().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);

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