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

// Load `.env` so operators can put CC_TEST_EMAIL / CC_TEST_PASSWORD /
// CC_BASE_URL / BACKEND_INTERNAL_URL there instead of remembering to export
// them in every shell. CLI env vars still win — dotenv does not override
// already-set process.env values.
dotenv.config()

/**
 * Playwright configuration for the Halo Bestie Control Center.
 *
 * Cross-machine flexibility is the priority: nothing here is hardcoded to a
 * specific host. The operator (or CI) starts the CC dev server + backend
 * separately and points these env vars at wherever they're reachable.
 *
 * Required env vars (with sensible local defaults):
 *   CC_BASE_URL          — where the CC SPA is served (default: http://localhost:5173)
 *   BACKEND_INTERNAL_URL — where the internal Fastify listener is served
 *                          (default: http://localhost:3001) — used by helpers,
 *                          not by Playwright directly.
 *   CC_TEST_EMAIL        — control-center user email (default: placeholder)
 *   CC_TEST_PASSWORD     — control-center user password (default: placeholder)
 *
 * Optional toggles:
 *   HEADED=1  — run with a visible browser (also: --headed CLI flag)
 *   RECORD=1  — record video for every test (default: off)
 *   PWDEBUG=1 — Playwright's built-in inspector
 *
 * NOTE: There is intentionally no `webServer` block — we never auto-start
 * the CC dev server. The operator controls when/where it runs so the same
 * Playwright suite can target a remote dev server on another machine.
 */

const CC_BASE_URL = process.env.CC_BASE_URL || 'http://localhost:5173'
const RECORD_VIDEO = process.env.RECORD === '1'

export default defineConfig({
  testDir: './tests/e2e',
  outputDir: './test-results',
  timeout: 30_000,
  expect: { timeout: 5_000 },

  // CC tests touch shared backend state (config rows, fixtures) — keep
  // serial by default to avoid flakes from parallel mutation.
  workers: 1,
  fullyParallel: false,

  reporter: [
    ['list'],
    ['html', { outputFolder: 'playwright-report', open: 'never' }],
  ],

  use: {
    baseURL: CC_BASE_URL,
    trace: 'retain-on-failure',
    screenshot: 'only-on-failure',
    video: RECORD_VIDEO ? 'on' : 'off',
    actionTimeout: 10_000,
    navigationTimeout: 15_000,
  },

  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    // To add firefox/webkit later:
    //   1. npx playwright install firefox webkit
    //   2. Add { name: 'firefox', use: { ...devices['Desktop Firefox'] } } here
  ],
})
