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

test.describe('Login Flow', () => {
  test('should login with valid credentials', async ({ page }) => {
    // Constants
    const testTrainerUsername = 'test_trainer';
    const testTrainerPassword = 'Test123!';
    
    // Create a login page instance
    const loginPage = new LoginPage(page);
    
    // Navigate to login page
    await loginPage.navigate();
    
    // Save a screenshot before login
    await page.screenshot({ path: 'screenshots/login-page-before.png' });
    
    // Verify login form is visible
    expect(await loginPage.isUsernameFieldVisible()).toBeTruthy();
    
    // Login with valid credentials
    await loginPage.login(testTrainerUsername, testTrainerPassword);
    
    // Wait for navigation
    await page.waitForLoadState('networkidle');
    
    // Check if logged in
    const isLoggedIn = await loginPage.isLoggedIn();
    console.log(`Is logged in: ${isLoggedIn}`);
    
    // Take a screenshot of the result
    await page.screenshot({ path: 'screenshots/login-success.png' });
    
    // Print current URL for debugging
    const currentUrl = await page.url();
    console.log('Current URL after login:', currentUrl);
    
    // Verify login was successful
    expect(isLoggedIn).toBeTruthy();
  });
  
  test('should show error with invalid credentials', async ({ page }) => {
    // Create a login page instance
    const loginPage = new LoginPage(page);
    
    // Navigate to login page
    await loginPage.navigate();
    
    // Login with invalid credentials
    await loginPage.login('invalid_user', 'wrong_password');
    
    // Check for error message
    const errorMessage = await loginPage.getErrorMessage();
    console.log(`Error message: ${errorMessage}`);
    
    // Take a screenshot of the error
    await page.screenshot({ path: 'screenshots/login-error.png' });
    
    // Expect an error message
    expect(errorMessage).not.toBeNull();
  });
});