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

const port = Number(process.env.NEXUS_E2E_PORT ?? 4175)
const baseURL = `http://127.0.0.1:${port}`

export default defineConfig({
  testDir: './e2e',
  testMatch: '**/*.e2e.ts',
  outputDir: 'test-results/e2e',
  fullyParallel: true,
  forbidOnly: Boolean(process.env.CI),
  retries: process.env.CI ? 2 : 0,
  // Vite transforms the route-split view graph on demand. A small bounded
  // worker pool keeps deep-link startup deterministic on developer machines;
  // CI stays serial to reduce variance further.
  workers: process.env.CI ? 1 : 2,
  reporter: [['list'], ['html', { outputFolder: 'playwright-report', open: 'never' }]],
  use: {
    baseURL,
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
  },
  projects: [
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        viewport: { width: 1440, height: 900 },
      },
    },
  ],
  webServer: {
    command: `pnpm exec vite --host 127.0.0.1 --port ${port} --strictPort`,
    url: baseURL,
    env: {
      VITE_BROWSER_TELEMETRY_ENABLED: 'false',
    },
    reuseExistingServer: !process.env.CI,
    timeout: 120_000,
  },
})
