import { Page, Locator, expect } from '@playwright/test';
import { BasePage } from './BasePage';

/**
 * Page object representing the Trainer Profile page
 */
export class ProfilePage extends BasePage {
  // Page elements
  readonly pageTitle: Locator;
  readonly personalInfoSection: Locator;
  readonly businessInfoSection: Locator;
  readonly trainingInfoSection: Locator;
  readonly statisticsSection: Locator;
  readonly editProfileButton: Locator;
  readonly dashboardLink: Locator;
  readonly createEventLink: Locator;
  readonly myEventsLink: Locator;
  readonly logoutLink: Locator;
  
  constructor(page: Page) {
    super(page);
    this.pageUrl = '/trainer-profile/';
    
    // Initialize locators
    this.pageTitle = page.locator('h1:has-text("Trainer Profile")');
    this.personalInfoSection = page.locator('section.hvac-profile-section:has(h2:text("Personal Information"))');
    this.businessInfoSection = page.locator('section.hvac-profile-section:has(h2:text("Business Information"))');
    this.trainingInfoSection = page.locator('section.hvac-profile-section:has(h2:text("Training Information"))');
    this.statisticsSection = page.locator('section.hvac-profile-section:has(h2:text("Training Statistics"))');
    this.editProfileButton = page.locator('a:has-text("Edit Profile")');
    this.dashboardLink = page.locator('a[href*="/hvac-dashboard/"]');
    this.createEventLink = page.locator('a[href*="/manage-event/"]');
    this.myEventsLink = page.locator('a[href*="/my-events/"]');
    this.logoutLink = page.locator('a:has-text("Logout")');
  }
  
  /**
   * Navigate to the profile page
   */
  async navigate() {
    await super.navigate();
    await this.page.waitForLoadState('networkidle');
    await expect(this.pageTitle).toBeVisible();
  }
  
  /**
   * Verify all sections are present
   */
  async verifyAllSectionsPresent() {
    await expect(this.personalInfoSection).toBeVisible();
    await expect(this.businessInfoSection).toBeVisible();
    await expect(this.trainingInfoSection).toBeVisible();
    await expect(this.statisticsSection).toBeVisible();
  }
  
  /**
   * Get personal information
   */
  async getPersonalInfo() {
    const displayName = await this.personalInfoSection.locator('h3').textContent();
    const email = await this.personalInfoSection.locator('p:has-text("Email:")').textContent();
    
    return {
      displayName: displayName?.trim(),
      email: email?.trim().replace('Email:', '').trim(),
    };
  }
  
  /**
   * Get business information
   */
  async getBusinessInfo() {
    const businessName = await this.businessInfoSection.locator('h3').textContent();
    const businessType = await this.businessInfoSection.locator('p:has-text("Type:")').textContent();
    
    return {
      businessName: businessName?.trim(),
      businessType: businessType?.trim().replace('Type:', '').trim(),
    };
  }
  
  /**
   * Get training statistics
   */
  async getTrainingStats() {
    const totalEvents = await this.statisticsSection.locator('.hvac-stat-card:has(h3:text("Total Events")) .metric-value').textContent();
    const upcomingEvents = await this.statisticsSection.locator('.hvac-stat-card:has(h3:text("Upcoming Events")) .metric-value').textContent();
    const pastEvents = await this.statisticsSection.locator('.hvac-stat-card:has(h3:text("Past Events")) .metric-value').textContent();
    const ticketsSold = await this.statisticsSection.locator('.hvac-stat-card:has(h3:text("Tickets Sold")) .metric-value').textContent();
    const totalRevenue = await this.statisticsSection.locator('.hvac-stat-card:has(h3:text("Total Revenue")) .metric-value').textContent();
    
    return {
      totalEvents: totalEvents?.trim(),
      upcomingEvents: upcomingEvents?.trim(),
      pastEvents: pastEvents?.trim(),
      ticketsSold: ticketsSold?.trim(),
      totalRevenue: totalRevenue?.trim(),
    };
  }
  
  /**
   * Click Edit Profile button
   */
  async clickEditProfile() {
    await this.editProfileButton.click();
    await this.page.waitForLoadState('networkidle');
  }
  
  /**
   * Navigate to dashboard
   */
  async goToDashboard() {
    await this.dashboardLink.click();
    await this.page.waitForLoadState('networkidle');
  }
  
  /**
   * Navigate to create event page
   */
  async goToCreateEvent() {
    await this.createEventLink.click();
    await this.page.waitForLoadState('networkidle');
  }
  
  /**
   * Navigate to my events page
   */
  async goToMyEvents() {
    await this.myEventsLink.click();
    await this.page.waitForLoadState('networkidle');
  }
  
  /**
   * Logout the user
   */
  async logout() {
    await this.logoutLink.click();
    await this.page.waitForLoadState('networkidle');
  }
}