import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './e2e',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  // ⚠️ CRITICAL: 1 worker pour éviter rate limiting backend (429)
  // Le backend a un rate limiter qui bloque trop de requêtes simultanées
  workers: 1,
  // 🔴 FIX: Augmenter le timeout global par défaut pour les tests d'upload
  timeout: 60000, // 60 secondes par défaut (peut être surchargé par test.setTimeout())
  
  // Global Setup: Login once and save authenticated state
  globalSetup: './e2e/global-setup.ts',
  
  reporter: [['html'], ['json', { outputFile: 'e2e-results.json' }]],
  use: {
    baseURL: process.env.PLAYWRIGHT_BASE_URL || process.env.VITE_FRONTEND_URL || 'http://localhost:5173',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    // Load authenticated state from global setup
    storageState: 'e2e/.auth/user.json',
  },
  projects: [
    {
      name: 'chromium',
      use: { 
        ...devices['Desktop Chrome'],
        // Each project can override storageState if needed
        storageState: 'e2e/.auth/user.json',
      },
    },
    {
      name: 'firefox',
      use: { 
        ...devices['Desktop Firefox'],
        storageState: 'e2e/.auth/user.json',
      },
    },
    {
      name: 'webkit',
      use: { 
        ...devices['Desktop Safari'],
        storageState: 'e2e/.auth/user.json',
      },
    },
    {
      name: 'msedge',
      use: { 
        ...devices['Desktop Edge'],
        storageState: 'e2e/.auth/user.json',
      },
    },
  ],
  webServer: {
    command: 'npm run dev',
    url: 'http://localhost:5173',
    reuseExistingServer: !process.env.CI,
    timeout: 120 * 1000,
  },
});

