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

// Default Playwright e2e configuration.
//
// Uses Firefox by default (Playwright Chromium crashes on macOS 15
// due to Crashpad permission restrictions).
//
// ## Usage
//
//   make playwright                   # run with default browser (Firefox)
//   PLAYWRIGHT_BROWSER=chromium make playwright
//   PLAYWRIGHT_BROWSER=webkit  make playwright
//
//   # Run a single test file:
//   PLAYWRIGHT_BROWSERS_PATH=assets/node_modules/playwright-core/.local-browsers \
//     npx playwright test --config=playwright.config.ts test/e2e/smoke.spec.ts
//
// ## Docker Chrome (alternative)
//
// Use playwright.docker.config.ts instead of this config:
//
//   docker compose -f docker-compose.chrome.yml up -d
//   npx playwright test --config=playwright.docker.config.ts

const browserEnv = process.env.PLAYWRIGHT_BROWSER || "firefox";

const deviceFor = (name: string) => {
  switch (name) {
    case "chromium": return devices["Desktop Chrome"];
    case "firefox":  return devices["Desktop Firefox"];
    case "webkit":   return devices["Desktop Safari"];
    default:
      console.warn(`Unknown browser "${name}", falling back to Firefox`);
      return devices["Desktop Firefox"];
  }
};

export default defineConfig({
  testDir: "./test/e2e",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: "list",

  use: {
    baseURL: process.env.PLAYWRIGHT_BASE_URL || "http://localhost:8056",
    trace: "on-first-retry",
    headless: process.env.PLAYWRIGHT_HEADLESS !== "false",
  },

  projects: [
    {
      name: browserEnv,
      use: { ...deviceFor(browserEnv) },
    },
  ],
});