import { STAGING_URL, PATHS, TIMEOUTS } from './config/staging-config';
import { test, expect } from '@playwright/test';

test.describe('Dashboard Shows Events After Fix', () => {
  test('dashboard should display event statistics', async ({ page }) => {
    const staging_url = 'https://upskill-staging.measurequick.com';
    
    // Login as test trainer
    await page.goto(`${staging_url}/community-login`);
    await page.fill('#user_login', 'test_trainer');
    await page.fill('#user_pass', 'password123!');
    await page.click('input[type="submit"]');
    await page.waitForTimeout(3000);

    // Go to dashboard
    await page.goto(`${staging_url}/hvac-dashboard`);
    await page.waitForLoadState('networkidle');
    
    // Take a screenshot for debugging
    await page.screenshot({ path: 'dashboard-with-stats.png' });
    
    // Check for the stats cards
    const statCards = await page.locator('.hvac-stat-card').count();
    console.log('Number of stat cards found:', statCards);
    expect(statCards).toBeGreaterThan(0);
    
    // Check for specific stats
    const totalEventsCard = await page.locator('.hvac-stat-card:has-text("Total Events")').isVisible();
    expect(totalEventsCard).toBe(true);
    
    // Get the total events value
    const totalEventsValue = await page.locator('.hvac-stat-card:has-text("Total Events") p').textContent();
    console.log('Total events value:', totalEventsValue);
    
    // Check if events are showing in the table
    const eventsTable = await page.locator('.events-table').isVisible();
    expect(eventsTable).toBe(true);
    
    // Count rows in the events table (excluding header)
    const eventRows = await page.locator('.events-table tbody tr').count();
    console.log('Number of event rows in table:', eventRows);
    
    // If the author fix is working, the total events value should match the table rows
    // (or at least show > 0 if events have been created by this user)
    const totalEventsNumber = parseInt(totalEventsValue || '0');
    console.log('Total events number:', totalEventsNumber);
    
    // Check if other stats are displayed
    const statsTexts = await page.locator('.hvac-stat-card h3').allTextContents();
    console.log('All stat card titles:', statsTexts);
    expect(statsTexts).toContain('Total Events');
    expect(statsTexts).toContain('Upcoming Events');
    expect(statsTexts).toContain('Past Events');
    expect(statsTexts).toContain('Tickets Sold');
    expect(statsTexts).toContain('Total Revenue');
  });
});