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

// Constants
// STAGING_URL is now imported from config
const LOGIN_URL = PATHS.login;
const DASHBOARD_URL = PATHS.dashboard;
const USERNAME = 'test_trainer';
const PASSWORD = 'Test123!';

test.describe('Certificate Functionality Tests', () => {
  test.beforeEach(async ({ page }) => {
    // Login before each test
    await page.goto(LOGIN_URL);
    await page.fill('#user_login', USERNAME);
    await page.fill('#user_pass', PASSWORD);
    await page.click('#wp-submit');
    await page.waitForLoadState('networkidle');
    
    // Verify login was successful
    await expect(page).toHaveURL(/hvac-dashboard/);
  });
  
  test('Should navigate to Generate Certificates page', async ({ page }) => {
    // Navigate to dashboard first
    await page.goto(DASHBOARD_URL);
    await page.waitForLoadState('networkidle');
    
    // Look for Generate Certificates link
    const generateLink = page.locator('a:has-text("Generate Certificates")');
    await expect(generateLink).toBeVisible();
    
    // Click the link
    await generateLink.click();
    await page.waitForLoadState('networkidle');
    
    // Check page title
    const title = await page.title();
    expect(title).toContain('Generate Certificates');
    
    // Check for event dropdown
    const eventDropdown = page.locator('#event_id');
    await expect(eventDropdown).toBeVisible();
    
    // Count options to verify dropdown is populated
    const optionCount = await page.locator('#event_id option').count();
    expect(optionCount).toBeGreaterThan(1);
  });
  
  test('Should navigate to Certificate Reports page', async ({ page }) => {
    // Navigate to dashboard first
    await page.goto(DASHBOARD_URL);
    await page.waitForLoadState('networkidle');
    
    // Look for Certificate Reports link
    const reportsLink = page.locator('a:has-text("Certificate Reports")');
    await expect(reportsLink).toBeVisible();
    
    // Click the link
    await reportsLink.click();
    await page.waitForLoadState('networkidle');
    
    // Check page title
    const title = await page.title();
    expect(title).toContain('Certificate Reports');
    
    // Check for filter form
    const filterForm = page.locator('form.hvac-certificate-filters');
    await expect(filterForm).toBeVisible();
  });
  
  test('Should filter certificates by event', async ({ page }) => {
    // Navigate to Certificate Reports page
    await page.goto(`${STAGING_URL}/certificate-reports/`);
    await page.waitForLoadState('networkidle');
    
    // Check for filter form
    const filterForm = page.locator('form.hvac-certificate-filters');
    await expect(filterForm).toBeVisible();
    
    // Check if event filter exists
    const eventFilter = page.locator('#filter_event');
    await expect(eventFilter).toBeVisible();
    
    // Get options count
    const optionCount = await page.locator('#filter_event option').count();
    
    // Test different filter options
    if (optionCount > 1) {
      // Select the first non-empty option
      await eventFilter.selectOption({ index: 1 });
      
      // Apply filter
      const filterButton = page.locator('button[type="submit"]');
      await filterButton.click();
      await page.waitForLoadState('networkidle');
      
      // Log the filter results (can't guarantee there will be certificates)
      const certificateItems = page.locator('.hvac-certificate-item');
      const certificateCount = await certificateItems.count();
      console.log(`Found ${certificateCount} certificates after filtering by event`);
    }
  });
  
  test('Should filter certificates by attendee', async ({ page }) => {
    // Navigate to Certificate Reports page
    await page.goto(`${STAGING_URL}/certificate-reports/`);
    await page.waitForLoadState('networkidle');
    
    // Check for filter form
    const filterForm = page.locator('form.hvac-certificate-filters');
    await expect(filterForm).toBeVisible();
    
    // Check if attendee search exists
    const attendeeSearch = page.locator('#search_attendee');
    
    // Some versions may not have this feature yet
    if (await attendeeSearch.isVisible()) {
      // Try a simple search
      await attendeeSearch.fill('test');
      
      // Apply filter
      const filterButton = page.locator('button[type="submit"]');
      await filterButton.click();
      await page.waitForLoadState('networkidle');
      
      // Verify filter was applied
      const currentSearchValue = await attendeeSearch.inputValue();
      expect(currentSearchValue).toBe('test');
      
      // Log the filter results
      const certificateItems = page.locator('.hvac-certificate-item');
      const certificateCount = await certificateItems.count();
      console.log(`Found ${certificateCount} certificates with attendee search "test"`);
      
      // Reset filters
      const resetButton = page.locator('button[type="reset"]');
      if (await resetButton.isVisible()) {
        await resetButton.click();
        await page.waitForLoadState('networkidle');
        
        // Verify reset worked
        const searchValue = await attendeeSearch.inputValue();
        expect(searchValue).toBe('');
      }
    } else {
      console.log('Attendee search not available, skipping test');
    }
  });
});