import path from "node:path";
import { fileURLToPath } from "node:url";

import { defineConfig, devices } from "@playwright/test";
import { config as loadEnv } from "dotenv";

const configDir = path.dirname(fileURLToPath(import.meta.url));
// Load local e2e env defaults from frontend/.env(.local), while keeping shell env highest priority.
loadEnv({ path: path.resolve(configDir, ".env.local") });
loadEnv({ path: path.resolve(configDir, ".env") });

const baseURL = process.env.FRONTEND_E2E_BASE_URL ?? "http://127.0.0.1:3000";

export default defineConfig({
  testDir: "./tests/e2e",
  timeout: 30_000,
  expect: {
    timeout: 10_000,
  },
  fullyParallel: true,
  retries: process.env.CI ? 1 : 0,
  reporter: process.env.CI ? [["list"], ["html", { open: "never" }]] : "list",
  use: {
    baseURL,
    trace: "on-first-retry",
    screenshot: "only-on-failure",
    video: "retain-on-failure",
  },
  projects: [
    {
      name: "chromium",
      use: { ...devices["Desktop Chrome"] },
    },
  ],
});
