import { test, expect } from '@playwright/test';
import { LoginPage } from './pages/LoginPage';
import { DashboardPage } from './pages/DashboardPage';

test.describe('Trainer Profile Page', () => {
  // Define test variables
  const testTrainerUsername = 'test_trainer';
  const testTrainerPassword = 'Test123!';
  
  test.beforeEach(async ({ page }) => {
    // Log in before each test
    const loginPage = new LoginPage(page);
    await loginPage.navigate();
    await loginPage.login(testTrainerUsername, testTrainerPassword);
  });
  
  test('should be accessible from dashboard', async ({ page }) => {
    // Navigate to dashboard
    const dashboardPage = new DashboardPage(page);
    await dashboardPage.navigate();
    
    // Click on the profile link
    const profileLink = page.locator('a[href*="/trainer-profile/"]');
    await expect(profileLink).toBeVisible();
    await profileLink.click();
    
    // Verify we're on the profile page
    await expect(page).toHaveURL(/.*\/trainer-profile\//);
    await expect(page.locator('h1:has-text("Trainer Profile")')).toBeVisible();
  });
  
  test('should display user personal information', async ({ page }) => {
    // Navigate directly to profile page
    await page.goto('/trainer-profile/');
    
    // Check for personal information section
    await expect(page.locator('h2:has-text("Personal Information")')).toBeVisible();
    
    // Check for basic user info
    const emailElement = page.locator('text=Email:');
    await expect(emailElement).toBeVisible();
    
    // Check for edit profile button
    const editProfileButton = page.locator('a:has-text("Edit Profile")');
    await expect(editProfileButton).toBeVisible();
  });
  
  test('should display business information', async ({ page }) => {
    // Navigate directly to profile page
    await page.goto('/trainer-profile/');
    
    // Check for business information section
    await expect(page.locator('h2:has-text("Business Information")')).toBeVisible();
    
    // Check for business name
    const businessNameHeading = page.locator('.hvac-business-details h3');
    await expect(businessNameHeading).toBeVisible();
  });
  
  test('should display training information', async ({ page }) => {
    // Navigate directly to profile page
    await page.goto('/trainer-profile/');
    
    // Check for training information section
    await expect(page.locator('h2:has-text("Training Information")')).toBeVisible();
    
    // Check for training categories
    const trainingAudienceHeading = page.locator('h4:has-text("Training Audience")');
    const trainingFormatsHeading = page.locator('h4:has-text("Training Formats")');
    const trainingLocationsHeading = page.locator('h4:has-text("Training Locations")');
    const trainingResourcesHeading = page.locator('h4:has-text("Training Resources")');
    
    await expect(trainingAudienceHeading).toBeVisible();
    await expect(trainingFormatsHeading).toBeVisible();
    await expect(trainingLocationsHeading).toBeVisible();
    await expect(trainingResourcesHeading).toBeVisible();
  });
  
  test('should display training statistics', async ({ page }) => {
    // Navigate directly to profile page
    await page.goto('/trainer-profile/');
    
    // Check for training statistics section
    await expect(page.locator('h2:has-text("Training Statistics")')).toBeVisible();
    
    // Check for statistics cards
    const totalEventsHeading = page.locator('h3:has-text("Total Events")');
    const upcomingEventsHeading = page.locator('h3:has-text("Upcoming Events")');
    const pastEventsHeading = page.locator('h3:has-text("Past Events")');
    const ticketsSoldHeading = page.locator('h3:has-text("Tickets Sold")');
    const totalRevenueHeading = page.locator('h3:has-text("Total Revenue")');
    
    await expect(totalEventsHeading).toBeVisible();
    await expect(upcomingEventsHeading).toBeVisible();
    await expect(pastEventsHeading).toBeVisible();
    await expect(ticketsSoldHeading).toBeVisible();
    await expect(totalRevenueHeading).toBeVisible();
  });
  
  test('should have working navigation links', async ({ page }) => {
    // Navigate directly to profile page
    await page.goto('/trainer-profile/');
    
    // Check for dashboard navigation link
    const dashboardLink = page.locator('a[href*="/hvac-dashboard/"]');
    await expect(dashboardLink).toBeVisible();
    
    // Check for create event link
    const createEventLink = page.locator('a[href*="/manage-event/"]');
    await expect(createEventLink).toBeVisible();
    
    // Check for my events link
    const myEventsLink = page.locator('a[href*="/my-events/"]');
    await expect(myEventsLink).toBeVisible();
    
    // Check for logout link
    const logoutLink = page.locator('a[href*="/wp-login.php?action=logout"]');
    await expect(logoutLink).toBeVisible();
  });
  
  test('should redirect to login page when not logged in', async ({ page }) => {
    // Log out first
    await page.goto('/wp-login.php?action=logout');
    await page.waitForURL(/.*\/community-login.*/);
    
    // Try to access profile page directly
    await page.goto('/trainer-profile/');
    
    // Should redirect to login page
    await expect(page).toHaveURL(/.*\/community-login.*/);
  });
});