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

test.describe('Trainer Profile Page (using Page Object Model)', () => {
  // 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 display all profile sections and data', async ({ page }) => {
    // Navigate to profile page
    const profilePage = new ProfilePage(page);
    await profilePage.navigate();
    
    // Verify all sections are present
    await profilePage.verifyAllSectionsPresent();
    
    // Get and verify personal information
    const personalInfo = await profilePage.getPersonalInfo();
    expect(personalInfo.displayName).toBeTruthy();
    expect(personalInfo.email).toBeTruthy();
    
    // Get and verify business information
    const businessInfo = await profilePage.getBusinessInfo();
    expect(businessInfo.businessName).toBeTruthy();
    
    // Get and verify training statistics
    const trainingStats = await profilePage.getTrainingStats();
    expect(trainingStats.totalEvents).toBeTruthy();
    expect(trainingStats.upcomingEvents).toBeTruthy();
    expect(trainingStats.pastEvents).toBeTruthy();
    expect(trainingStats.ticketsSold).toBeTruthy();
    expect(trainingStats.totalRevenue).toBeTruthy();
    
    // Save screenshot for verification
    await page.screenshot({ path: './test-results/trainer-profile-full.png', fullPage: true });
  });
  
  test('should allow navigation to other pages', async ({ page }) => {
    // Navigate to profile page
    const profilePage = new ProfilePage(page);
    await profilePage.navigate();
    
    // Test navigation to dashboard
    await profilePage.goToDashboard();
    await expect(page).toHaveURL(/.*\/hvac-dashboard\//);
    
    // Return to profile page
    await profilePage.navigate();
    
    // Test navigation to create event
    await profilePage.goToCreateEvent();
    await expect(page).toHaveURL(/.*\/manage-event\//);
    
    // Return to profile page
    await profilePage.navigate();
    
    // Test navigation to my events
    await profilePage.goToMyEvents();
    await expect(page).toHaveURL(/.*\/my-events\//);
  });
  
  test('should navigate between dashboard and profile', async ({ page }) => {
    // Go to dashboard first
    const dashboardPage = new DashboardPage(page);
    await dashboardPage.navigate();
    
    // Find and click profile link on dashboard
    await page.locator('a[href*="/trainer-profile/"]').click();
    await expect(page).toHaveURL(/.*\/trainer-profile\//);
    
    // Verify we're on the profile page
    const profilePage = new ProfilePage(page);
    await expect(profilePage.pageTitle).toBeVisible();
    
    // Go back to dashboard
    await profilePage.goToDashboard();
    await expect(page).toHaveURL(/.*\/hvac-dashboard\//);
    await expect(dashboardPage.pageTitle).toBeVisible();
  });
  
  test('should redirect when not logged in', async ({ page }) => {
    // Create a new context without login
    const profilePage = new ProfilePage(page);
    
    // Logout first
    await page.goto('/wp-login.php?action=logout');
    await page.waitForURL(/.*\/community-login.*/);
    
    // Try to navigate to profile page
    await page.goto('/trainer-profile/');
    
    // Should redirect to login page
    await expect(page).toHaveURL(/.*\/community-login.*/);
  });
});