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

/**
 * EN: E2E tests for authentication flows
 * VI: E2E tests cho các luồng xác thực
 */
test.describe('Authentication', () => {
  test.beforeEach(async ({ page }) => {
    // EN: Navigate to login page / VI: Điều hướng đến trang login
    await page.goto('/login');
  });

  test('should display login page', async ({ page }) => {
    // EN: Check for heading "Sign In" / VI: Kiểm tra heading "Sign In"
    await expect(page.getByRole('heading', { name: 'Sign In' })).toBeVisible();
    // EN: Check for email input with exact placeholder / VI: Kiểm tra input email với placeholder chính xác
    await expect(page.getByPlaceholder('you@example.com')).toBeVisible();
    // EN: Check for password input with placeholder / VI: Kiểm tra input password với placeholder
    await expect(page.getByPlaceholder('Password')).toBeVisible();
  });

  test('should show validation errors for empty form', async ({ page }) => {
    const submitButton = page.getByRole('button', { name: 'Sign In' });
    await submitButton.click();
    // EN: Wait for validation to complete / VI: Đợi validation hoàn thành
    await page.waitForTimeout(500);
    // EN: Check for validation errors (email or password required) / VI: Kiểm tra lỗi validation (email hoặc password required)
    const emailError = page.getByText('Email is required');
    const passwordError = page.getByText('Password is required');
    // EN: At least one error should be visible / VI: Ít nhất một lỗi phải hiển thị
    const hasError = await emailError.isVisible().catch(() => false) ||
      await passwordError.isVisible().catch(() => false);
    expect(hasError).toBeTruthy();
  });

  test('should navigate to register page', async ({ page }) => {
    await page.getByRole('link', { name: 'Sign up' }).click();
    await expect(page).toHaveURL(/.*\/register/);
  });

  test('should navigate to forgot password page', async ({ page }) => {
    await page.getByRole('link', { name: 'Forgot password?' }).click();
    await expect(page).toHaveURL(/.*\/forgot-password/);
  });
});
