import { test, expect } from '../fixtures';
import { TestDataManager } from '../utils/test-data-manager';

/**
 * Authentication Tests
 * Covers: Login, Register, Logout, Token Refresh, API Keys
 * Target: 100% coverage on critical auth paths
 */

test.describe('Authentication @auth @critical', () => {
  
  test('should login with valid credentials', async ({ page }) => {
    // Arrange
    const email = `test_${Date.now()}@example.com`;
    const password = 'TestPassword123!';
    
    // First register a user
    await page.goto('/register');
    await page.fill('[data-testid="full-name-input"]', 'Test User');
    await page.fill('[data-testid="email-input"]', email);
    await page.fill('[data-testid="password-input"]', password);
    await page.fill('[data-testid="confirm-password-input"]', password);
    await page.click('[data-testid="register-button"]');
    
    // Wait for redirect to login
    await page.waitForURL('/login');
    
    // Login
    await page.fill('[data-testid="email-input"]', email);
    await page.fill('[data-testid="password-input"]', password);
    await page.click('[data-testid="login-button"]');
    
    // Assert
    await page.waitForURL('/dashboard');
    await expect(page.locator('[data-testid="user-menu"]')).toBeVisible();
    await expect(page.locator('[data-testid="dashboard-header"]')).toContainText('Dashboard');
  });

  test('should show error for invalid credentials', async ({ page }) => {
    await page.goto('/login');
    await page.fill('[data-testid="email-input"]', 'invalid@example.com');
    await page.fill('[data-testid="password-input"]', 'wrongpassword');
    await page.click('[data-testid="login-button"]');
    
    await expect(page.locator('[data-testid="error-message"]')).toBeVisible();
    await expect(page.locator('[data-testid="error-message"]')).toContainText('Invalid credentials');
    await expect(page).toHaveURL('/login');
  });

  test('should validate registration form', async ({ page }) => {
    await page.goto('/register');
    await page.click('[data-testid="register-button"]');
    
    // Assert validation errors
    await expect(page.locator('[data-testid="email-error"]')).toBeVisible();
    await expect(page.locator('[data-testid="password-error"]')).toBeVisible();
    await expect(page.locator('[data-testid="confirm-password-error"]')).toBeVisible();
  });

  test('should logout successfully', async ({ authenticatedPage }) => {
    await authenticatedPage.click('[data-testid="user-menu"]');
    await authenticatedPage.click('[data-testid="logout-button"]');
    
    await authenticatedPage.waitForURL('/login');
    await expect(authenticatedPage.locator('[data-testid="login-form"]')).toBeVisible();
  });

  test('should refresh token automatically', async ({ page, testData }) => {
    // Login
    const user = await testData.createTestUser();
    await page.goto('/login');
    await page.fill('[data-testid="email-input"]', user.email);
    await page.fill('[data-testid="password-input"]', user.password);
    await page.click('[data-testid="login-button"]');
    await page.waitForURL('/dashboard');
    
    // Navigate to protected page after token should refresh
    await page.goto('/scenarios');
    await expect(page.locator('[data-testid="scenarios-list"]')).toBeVisible();
  });

  test('should prevent access to protected routes when not authenticated', async ({ page }) => {
    await page.goto('/dashboard');
    await page.waitForURL('/login?redirect=/dashboard');
    await expect(page.locator('[data-testid="login-form"]')).toBeVisible();
  });

  test('should persist session across page reloads', async ({ authenticatedPage }) => {
    await authenticatedPage.reload();
    await expect(authenticatedPage.locator('[data-testid="dashboard-header"]')).toBeVisible();
    await expect(authenticatedPage.locator('[data-testid="user-menu"]')).toBeVisible();
  });

  test.describe('Password Reset', () => {
    test('should send password reset email', async ({ page }) => {
      await page.goto('/forgot-password');
      await page.fill('[data-testid="email-input"]', 'user@example.com');
      await page.click('[data-testid="send-reset-button"]');
      
      await expect(page.locator('[data-testid="success-message"]')).toBeVisible();
      await expect(page.locator('[data-testid="success-message"]')).toContainText('Check your email');
    });

    test('should validate reset token', async ({ page }) => {
      await page.goto('/reset-password?token=invalid');
      await expect(page.locator('[data-testid="invalid-token-error"]')).toBeVisible();
    });
  });
});

test.describe('API Key Management @api-keys @critical', () => {
  
  test('should create new API key', async ({ authenticatedPage }) => {
    await authenticatedPage.goto('/settings/api-keys');
    await authenticatedPage.click('[data-testid="create-api-key-button"]');
    await authenticatedPage.fill('[data-testid="api-key-name-input"]', 'Test API Key');
    await authenticatedPage.fill('[data-testid="api-key-description-input"]', 'For E2E testing');
    await authenticatedPage.click('[data-testid="save-api-key-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="api-key-created-dialog"]')).toBeVisible();
    await expect(authenticatedPage.locator('[data-testid="api-key-value"]')).toBeVisible();
  });

  test('should revoke API key', async ({ authenticatedPage }) => {
    // First create an API key
    await authenticatedPage.goto('/settings/api-keys');
    await authenticatedPage.click('[data-testid="create-api-key-button"]');
    await authenticatedPage.fill('[data-testid="api-key-name-input"]', 'Key to Revoke');
    await authenticatedPage.click('[data-testid="save-api-key-button"]');
    await authenticatedPage.click('[data-testid="close-dialog-button"]');
    
    // Revoke it
    await authenticatedPage.click('[data-testid="revoke-key-button"]').first();
    await authenticatedPage.click('[data-testid="confirm-revoke-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="key-revoked-success"]')).toBeVisible();
  });

  test('should copy API key to clipboard', async ({ authenticatedPage, context }) => {
    await context.grantPermissions(['clipboard-read', 'clipboard-write']);
    
    await authenticatedPage.goto('/settings/api-keys');
    await authenticatedPage.click('[data-testid="create-api-key-button"]');
    await authenticatedPage.fill('[data-testid="api-key-name-input"]', 'Copy Test');
    await authenticatedPage.click('[data-testid="save-api-key-button"]');
    await authenticatedPage.click('[data-testid="copy-api-key-button"]');
    
    await expect(authenticatedPage.locator('[data-testid="copy-success-toast"]')).toBeVisible();
  });
});
