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

/**
 * Playwright configuration for E2E tests.
 * Tests live streaming flow: Mobile Safari (publisher) → Desktop Chrome/Firefox (viewer)
 */
export default defineConfig({
  testDir: './test/e2e',
  fullyParallel: false, // WebRTC tests need sequential execution
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 1,
  workers: 1, // Run tests sequentially for WebRTC
  reporter: 'html',
  globalSetup: './test/e2e/global-setup.js',
  globalTeardown: './test/e2e/global-teardown.js',
  use: {
    baseURL: 'https://localhost:3000',
    ignoreHTTPSErrors: true, // Accept self-signed certs from .dev-certs/
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
  },
  projects: [
    {
      name: 'mobile-safari',
      use: {
        ...devices['iPhone 13'],
        // Use WebKit for Safari-like behavior (actual Safari requires macOS)
        channel: 'webkit',
        permissions: ['geolocation'],
        geolocation: { latitude: 37.7749, longitude: -122.4194 },
      },
    },
    {
      name: 'desktop-chrome',
      use: {
        ...devices['Desktop Chrome'],
        permissions: ['camera', 'microphone', 'geolocation'],
        launchOptions: {
          args: [
            '--use-fake-ui-for-media-stream',
            '--use-fake-device-for-media-stream',
          ],
        },
      },
    },
    {
      name: 'desktop-firefox',
      use: {
        ...devices['Desktop Firefox'],
      },
    },
  ],
  webServer: {
    command: 'npm run dev',
    url: 'https://localhost:3000/health/ready',
    reuseExistingServer: !process.env.CI, // Don't reuse in CI (always start fresh)
    timeout: 180_000, // 3 minutes (180 seconds) for server startup (CI can be slower)
    ignoreHTTPSErrors: true,
  },
  timeout: process.env.CI ? 180_000 : 60_000, // 3 minutes in CI, 1 minute locally (WebRTC setup takes time)
})
