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

test.describe('Auth Flow', () => {
  test('login success - redirects to home and stores token', async ({ page }) => {
    await page.goto('/login');
    
    // Fill login form
    await page.fill('#loginUsername', 'testuser');
    await page.fill('#loginPassword', 'password123');
    
    // Click login button
    await page.click('#loginSubmit');
    
    // Wait for redirect or success message
    await page.waitForTimeout(2000);
    
    // Check if redirected or success message shown
    const currentUrl = page.url();
    const successMessage = await page.locator('#authSuccess').textContent().catch(() => '');
    
    // Either redirect happened or success message shown
    expect(currentUrl.includes('/web') || successMessage.includes('réussie')).toBeTruthy();
  });

  test('login with wrong credentials shows error', async ({ page }) => {
    await page.goto('/login');
    
    // Fill login form with wrong credentials
    await page.fill('#loginUsername', 'nonexistentuser');
    await page.fill('#loginPassword', 'wrongpassword');
    
    // Click login button
    await page.click('#loginSubmit');
    
    // Wait for error
    await page.waitForTimeout(2000);
    
    // Check error message is displayed
    const errorVisible = await page.locator('#authError').isVisible().catch(() => false);
    const errorText = await page.locator('#authError').textContent().catch(() => '');
    
    // Error should be shown (and NOT be "[object Object]")
    expect(errorVisible || errorText.length > 0).toBeTruthy();
    expect(errorText).not.toContain('[object Object]');
  });

  test('register new user shows success', async ({ page }) => {
    await page.goto('/login');
    
    // Switch to register tab
    await page.click('text=Inscription');
    
    // Fill register form with unique username
    const uniqueUsername = 'testuser_' + Date.now();
    await page.fill('#registerUsername', uniqueUsername);
    await page.fill('#registerPassword', 'password123');
    await page.fill('#registerPasswordConfirm', 'password123');
    
    // Click register button
    await page.click('#registerSubmit');
    
    // Wait for success
    await page.waitForTimeout(2000);
    
    // Check success message
    const successVisible = await page.locator('#authSuccess').isVisible().catch(() => false);
    const successText = await page.locator('#authSuccess').textContent().catch(() => '');
    
    // Success should be shown
    expect(successVisible || successText.includes('réussie')).toBeTruthy();
  });

  test('password mismatch shows validation error', async ({ page }) => {
    await page.goto('/login');
    
    // Switch to register tab
    await page.click('text=Inscription');
    
    // Fill register form with mismatching passwords
    await page.fill('#registerUsername', 'testuser');
    await page.fill('#registerPassword', 'password123');
    await page.fill('#registerPasswordConfirm', 'differentpassword');
    
    // Click register button
    await page.click('#registerSubmit');
    
    // Wait for error
    await page.waitForTimeout(1000);
    
    // Check error message
    const errorText = await page.locator('#authError').textContent().catch(() => '');
    
    // Should show password mismatch error
    expect(errorText).toContain('correspondent');
  });

  test('login button shows loading state during request', async ({ page }) => {
    await page.goto('/login');
    
    // Get button and check initial state
    const button = page.locator('#loginSubmit');
    const initialText = await button.textContent();
    
    // Fill form and click
    await page.fill('#loginUsername', 'testuser');
    await page.fill('#loginPassword', 'password123');
    
    // Click and immediately check loading state
    await button.click();
    
    // Check loading state (should change text or be disabled)
    await page.waitForTimeout(100);
    const buttonText = await button.textContent();
    const isDisabled = await button.isDisabled().catch(() => false);
    
    // Button should either show loading text or be disabled
    expect(buttonText !== initialText || isDisabled).toBeTruthy();
  });
});
