/**
 * @fileoverview E2E Tests for trainer profile editing functionality.
 * This test suite verifies that trainers can view, edit, and update their profiles.
 */

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

// Test data
const TEST_USER = {
    username: process.env.TEST_TRAINER_USERNAME || 'test_trainer',
    password: process.env.TEST_TRAINER_PASSWORD || 'Test_password123'
};

const UPDATED_INFO = {
    firstName: 'Updated',
    lastName: 'Trainer',
    displayName: 'Updated Trainer Profile',
    bio: 'This is an updated bio from the automated test.',
    businessName: 'Updated Training Co.',
    businessPhone: '555-123-4567',
    businessEmail: 'updated@example.com',
    businessDesc: 'This is an updated business description from the automated test.'
};

test.describe('Trainer Profile Editing @trainer-profile', () => {
    test.beforeEach(async ({ page }) => {
        // Login before each test
        const loginPage = new LoginPage(page);
        await loginPage.goto();
        await loginPage.login(TEST_USER.username, TEST_USER.password);
        
        // Verify we're logged in by checking if we're on the dashboard
        const dashboardPage = new DashboardPage(page);
        await expect(page).toHaveURL(/.*hvac-dashboard.*/);
        await expect(dashboardPage.welcomeMessage).toBeVisible();
    });

    test('should navigate to edit profile page', async ({ page }) => {
        // Navigate to trainer profile page first
        await page.goto('/trainer-profile/');
        await expect(page).toHaveURL(/.*trainer-profile.*/);
        await expect(page.locator('h1')).toContainText('Trainer Profile');
        
        // Click the edit profile button
        await page.click('text=Edit Profile');
        
        // Verify we're on the edit profile page
        await expect(page).toHaveURL(/.*edit-profile.*/);
        await expect(page.locator('h1')).toContainText('Edit Trainer Profile');
        
        // Verify form is loaded with user's current info
        await expect(page.locator('#first_name')).toBeVisible();
        await expect(page.locator('#last_name')).toBeVisible();
        await expect(page.locator('#display_name')).toBeVisible();
    });

    test('should update basic profile information', async ({ page }) => {
        // Go directly to edit profile page
        await page.goto('/edit-profile/');
        
        // Clear and update form fields
        await page.locator('#first_name').fill(UPDATED_INFO.firstName);
        await page.locator('#last_name').fill(UPDATED_INFO.lastName);
        await page.locator('#display_name').fill(UPDATED_INFO.displayName);
        await page.locator('#description').fill(UPDATED_INFO.bio);
        
        // Submit the form
        await page.click('button[name="hvac_update_profile"]');
        
        // Verify success message
        await expect(page.locator('.hvac-message-success')).toBeVisible();
        await expect(page.locator('.hvac-message-success')).toContainText('Your profile has been updated successfully');
        
        // Navigate to profile page to verify changes
        await page.goto('/trainer-profile/');
        
        // Verify updated information is displayed
        await expect(page.locator('.trainer-name')).toContainText(UPDATED_INFO.displayName);
        await expect(page.locator('.trainer-bio')).toContainText(UPDATED_INFO.bio);
    });

    test('should update business information', async ({ page }) => {
        // Go to edit profile page
        await page.goto('/edit-profile/');
        
        // Update business information
        await page.locator('#business_name').fill(UPDATED_INFO.businessName);
        await page.locator('#business_phone').fill(UPDATED_INFO.businessPhone);
        await page.locator('#business_email').fill(UPDATED_INFO.businessEmail);
        await page.locator('#business_description').fill(UPDATED_INFO.businessDesc);
        
        // Submit the form
        await page.click('button[name="hvac_update_profile"]');
        
        // Verify success message
        await expect(page.locator('.hvac-message-success')).toBeVisible();
        
        // Navigate to profile page to verify changes
        await page.goto('/trainer-profile/');
        
        // Verify updated business information is displayed
        await expect(page.locator('.business-name')).toContainText(UPDATED_INFO.businessName);
        await expect(page.locator('.business-info')).toContainText(UPDATED_INFO.businessPhone);
        await expect(page.locator('.business-info')).toContainText(UPDATED_INFO.businessEmail);
        await expect(page.locator('.business-description')).toContainText(UPDATED_INFO.businessDesc);
    });

    test('should display appropriate validation errors', async ({ page }) => {
        // Go to edit profile page
        await page.goto('/edit-profile/');
        
        // Clear required fields
        await page.locator('#first_name').fill('');
        await page.locator('#last_name').fill('');
        await page.locator('#user_email').fill('');
        
        // Submit the form
        await page.click('button[name="hvac_update_profile"]');
        
        // Verify validation errors
        await expect(page.locator('#first_name_error')).toBeVisible();
        await expect(page.locator('#last_name_error')).toBeVisible();
        await expect(page.locator('#user_email_error')).toBeVisible();
        
        // Invalid email format
        await page.locator('#user_email').fill('not-an-email');
        await page.click('button[name="hvac_update_profile"]');
        await expect(page.locator('#user_email_error')).toBeVisible();
        await expect(page.locator('#user_email_error')).toContainText('valid email');
    });

    test('should validate password change fields', async ({ page }) => {
        // Go to edit profile page
        await page.goto('/edit-profile/');
        
        // Enter incorrect current password
        await page.locator('#current_password').fill('wrong-password');
        await page.locator('#new_password').fill('NewPassword123');
        await page.locator('#confirm_new_password').fill('NewPassword123');
        
        // Submit the form
        await page.click('button[name="hvac_update_profile"]');
        
        // Verify error message for incorrect current password
        await expect(page.locator('#current_password_error')).toBeVisible();
        await expect(page.locator('#current_password_error')).toContainText('incorrect');
        
        // Test password mismatch
        await page.locator('#current_password').fill(TEST_USER.password);
        await page.locator('#new_password').fill('NewPassword123');
        await page.locator('#confirm_new_password').fill('DifferentPassword123');
        
        // Submit the form
        await page.click('button[name="hvac_update_profile"]');
        
        // Verify error message for password mismatch
        await expect(page.locator('#confirm_new_password_error')).toBeVisible();
        await expect(page.locator('#confirm_new_password_error')).toContainText('do not match');
    });
});