import { defineConfig, devices } from "@playwright/test";
import * as path from "node:path";

// Defaults so `pnpm test:e2e:ui` runs standalone. Must match apps/edr-passenger-api/.env.
process.env.DATABASE_URL ??=
  "postgresql://edr:edr_secret@localhost:5544/edr_database?schema=passenger";
process.env.JWT_ACCESS_TOKEN_SECRET ??= "test-access-secret-0000000000000000000000";
process.env.DEFAULT_PASSWORD ??= "Test@1234";

/**
 * Playwright UI E2E for the EDR passenger platform.
 * Track A (portal booking combinations) + Track B (backoffice config → portal propagation).
 * See docs/ui-e2e-test-matrix.md. global-setup boots/awaits the stack, seeds the 5544 test DB,
 * and mints the passenger + staff storageStates.
 */
const PORTAL = process.env.PORTAL_URL ?? "http://localhost:5174";
const BACKOFFICE = process.env.BACKOFFICE_URL ?? "http://localhost:5184";
const STORAGE = path.join(__dirname, "fixtures", "storage");

export default defineConfig({
  testDir: path.join(__dirname, "specs"),
  fullyParallel: false, // shared seeded DB — serialize to keep assertions deterministic
  workers: 1,
  retries: 0,
  timeout: 60_000,
  expect: { timeout: 10_000 },
  globalSetup: path.join(__dirname, "global-setup.ts"),
  reporter: [
    ["list"],
    ["html", { outputFolder: path.join(__dirname, "..", "e2e-ui-report"), open: "never" }],
  ],
  // Boot the app tier automatically; reuse it if it's already running (dev). Infra (Postgres 5544,
  // RabbitMQ, migrations, @edr/types build) is handled by e2e-ui/run.sh BEFORE Playwright starts.
  webServer: [
    {
      command: "pnpm --filter @edr/passenger-api dev",
      url: "http://localhost:4000/stations",
      timeout: 180_000,
      reuseExistingServer: true,
      env: { GITHUB_PACKAGE_TOKEN: process.env.GITHUB_PACKAGE_TOKEN ?? "dummy" },
    },
    {
      command: "pnpm --filter @edr/passenger-portal dev",
      url: PORTAL,
      timeout: 120_000,
      reuseExistingServer: true,
      env: { GITHUB_PACKAGE_TOKEN: process.env.GITHUB_PACKAGE_TOKEN ?? "dummy" },
    },
    {
      command: "pnpm --filter @edr/passenger-backoffice dev",
      url: `${BACKOFFICE}/login`,
      timeout: 120_000,
      reuseExistingServer: true,
      env: { GITHUB_PACKAGE_TOKEN: process.env.GITHUB_PACKAGE_TOKEN ?? "dummy" },
    },
  ],
  use: {
    trace: "retain-on-failure",
    screenshot: "only-on-failure",
    actionTimeout: 15_000,
    // SLOWMO=500 bash e2e-ui/run.sh --headed  → pause 500ms between each browser action
    launchOptions: { slowMo: Number(process.env.SLOWMO ?? 0) },
  },
  projects: [
    {
      name: "portal", // Track A — logged-in passenger
      testMatch: /specs\/portal\/.*\.spec\.ts/,
      use: {
        ...devices["Desktop Chrome"],
        baseURL: PORTAL,
        storageState: path.join(STORAGE, "passenger.json"),
      },
    },
    {
      name: "guest", // Track A — guest bookings (no auth)
      testMatch: /specs\/guest\/.*\.spec\.ts/,
      use: { ...devices["Desktop Chrome"], baseURL: PORTAL },
    },
    {
      name: "backoffice", // Track B — staff/admin
      testMatch: /specs\/backoffice\/.*\.spec\.ts/,
      use: {
        ...devices["Desktop Chrome"],
        baseURL: BACKOFFICE,
        storageState: path.join(STORAGE, "staff.json"),
      },
    },
    {
      name: "propagation", // cross-app: staff writes config via API → passenger portal reads
      testMatch: /specs\/propagation\/.*\.spec\.ts/,
      use: { ...devices["Desktop Chrome"], baseURL: PORTAL },
    },
  ],
});
