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

const webPort = process.env.E2E_WEB_PORT ?? "5175";
const webUrl = process.env.E2E_BASE_URL ?? `http://127.0.0.1:${webPort}`;

const apiPort = process.env.E2E_API_PORT ?? "8001";
const apiUrl = process.env.E2E_API_URL ?? `http://127.0.0.1:${apiPort}`;
const startApi = process.env.E2E_START_API !== "false";

export default defineConfig({
  testDir: "./e2e",
  retries: process.env.CI ? 1 : 0,
  workers: 1,
  webServer: startApi
    ? [
        {
          command: "python scripts/start_e2e_api.py",
          cwd: "../api",
          url: `${apiUrl}/api/v1/health`,
          reuseExistingServer: !process.env.CI,
          timeout: 120_000,
          env: {
            E2E_API_PORT: apiPort,
            E2E_WEB_PORT: webPort
          }
        },
        {
          command: `npx vite --host 127.0.0.1 --port ${webPort}`,
          url: webUrl,
          reuseExistingServer: !process.env.CI,
          env: {
            VITE_USE_API_PROXY: "true",
            VITE_API_URL: apiUrl
          }
        }
      ]
    : {
        command: `npx vite --host 127.0.0.1 --port ${webPort}`,
        url: webUrl,
        reuseExistingServer: !process.env.CI
      },
  use: {
    baseURL: webUrl,
    trace: "on-first-retry",
    video: "on-first-retry"
  },
  projects: [{ name: "chromium", use: { browserName: "chromium" } }]
});
