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

/**
 * Playwright e2e smoke suite (M8). Boots the real single-process app
 * (FastAPI serving the built React SPA + /api) against a DEDICATED,
 * migrated + seeded e2e Postgres, using the dev identity stub so every
 * request resolves to `rian` (the owner) with no login.
 *
 * NOT part of `make check`: browsers aren't installed in CI and this needs a
 * seeded database. See docs/E2E.md. Run it explicitly with `make e2e` after
 * `npx playwright install chromium` and exporting WITH_DB_URL.
 *
 * @playwright/test lives in frontend/node_modules; invoke from there with an
 * explicit `--config ../playwright.config.ts` (the `e2e` npm script does
 * exactly this). Playwright anchors `testDir` and `webServer.cwd` to THIS
 * file's directory (the repo root), regardless of the invoking cwd.
 */

const PORT = 8099;
const BASE_URL = `http://127.0.0.1:${PORT}`;

export default defineConfig({
  testDir: './e2e',
  testMatch: '**/*.spec.ts',

  // Specs mutate a shared database (imports, invoices, detach) — run them
  // serially so state from one can't race another.
  fullyParallel: false,
  workers: 1,

  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 1 : 0,
  timeout: 60_000, // per test
  expect: { timeout: 10_000 }, // web-first assertion polling budget
  reporter: process.env.CI ? [['github'], ['list']] : [['list']],

  use: {
    baseURL: BASE_URL,
    headless: true,
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    // The invoice paste block copies to the clipboard; grant it so the
    // verified-copy toast reports success rather than "Copy failed…".
    permissions: ['clipboard-read', 'clipboard-write'],
  },

  projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],

  webServer: {
    // Reference the host venv uvicorn (Makefile `venv` target provisions it).
    // Resolved relative to this config's directory (the repo root).
    command: '.venv/bin/uvicorn app.main:app --host 127.0.0.1 --port ' + PORT,
    url: BASE_URL,
    reuseExistingServer: !process.env.CI,
    timeout: 120_000, // first boot compiles nothing but connects to Postgres
    stdout: 'pipe',
    stderr: 'pipe',
    env: {
      // Dev identity stub → every request is `rian`, the seeded owner. The
      // app REFUSES to start if AUTH_DEV_USER is set while APP_ENV=production
      // (T-AZ-043), so APP_ENV must be a non-production value.
      AUTH_DEV_USER: 'rian',
      APP_ENV: 'test',
      // No nightly-snapshot background job during tests.
      SNAPSHOT_ENABLED: 'false',
      // REQUIRED: the operator exports WITH_DB_URL pointing at a dedicated
      // migrated + seeded e2e Postgres (postgresql+psycopg://…). It's
      // inherited from the shell here — the app reads it and overrides the
      // repo .env value. See docs/E2E.md. Uvicorn run directly does NOT run
      // `alembic upgrade head`, so the target DB must already be migrated.
      WITH_DB_URL: process.env.WITH_DB_URL ?? '',
    },
  },
});
