import { defineConfig } from "@playwright/test";

// Default to the production server so Playwright tests hit a realistic,
// pre-compiled build instead of Turbopack's cold-compile dev mode, which
// makes cold route visits too slow to fit inside a per-test timeout.
// Set E2E_PROD_SERVER=0 to fall back to `pnpm dev` during local debugging.
const useProdServer = process.env.E2E_PROD_SERVER !== "0";

export default defineConfig({
  testDir: "./tests/e2e",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : 4,
  reporter: "list",
  timeout: 60_000,
  expect: { timeout: 10_000 },
  use: {
    baseURL: "http://localhost:3000",
    trace: "on-first-retry",
    navigationTimeout: 45_000,
    actionTimeout: 15_000,
  },
  webServer: {
    command: useProdServer ? "pnpm build && pnpm start" : "pnpm dev",
    url: "http://localhost:3000",
    reuseExistingServer: !process.env.CI,
    timeout: 300_000,
    env: {
      SEED_ADMIN_PASSWORD:
        process.env.SEED_ADMIN_PASSWORD ?? "changeme-after-first-login",
    },
  },
});
