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

export default defineConfig({
  testDir: './tests/e2e',
  timeout: 30 * 1000,
  expect: {
    timeout: 5000
  },
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: 'html',
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
  },

  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
  ],

  webServer: [
    {
      command: 'npm run dev',
      port: 3000,
      reuseExistingServer: !process.env.CI,
    },
    {
      command: 'cd ../backend && poetry run uvicorn app.main:app --host 127.0.0.1 --port 8000',
      port: 8000,
      reuseExistingServer: !process.env.CI,
      env: {
        APP_ENV: 'test',
        MONGODB_URI: 'mongodb://localhost:27017',
        MONGODB_DB: 'test_accessible_video_e2e',
        REDIS_URL: 'redis://localhost:6379',
        JWT_SECRET: 'test_secret_for_e2e',
        GEMINI_API_KEY: 'fake_key_for_e2e',
        GCP_PROJECT_ID: 'test-project',
        GCS_BUCKET: 'test-bucket',
        SENDGRID_API_KEY: 'fake_sendgrid',
        EMAIL_FROM: 'test@example.com',
        CLIENT_BASE_URL: 'http://localhost:3000',
      },
    },
  ],
});