import { RunMode, getChecksumConfig } from "@checksum-ai/runtime";
import * as path from "path";
import * as dotenv from "dotenv";

// Load env vars from the project-root .env first, then checksum/.env. The root
// is derived from this file's location (checksum/ lives at <project>/checksum),
// NOT from process.cwd(), so it resolves correctly no matter which directory
// Playwright/the CLI is launched from. dotenv does not overwrite variables that
// are already set, so the root .env wins on any overlapping key and
// checksum/.env only fills in the rest (and anything already exported in the
// real shell environment beats both files).
const projectRoot = path.join(__dirname, "..");
dotenv.config({ path: path.join(projectRoot, ".env") });
dotenv.config({ path: path.join(__dirname, ".env") });

export default getChecksumConfig({
  /**
   * Checksum Run mode. See Readme for more info
   */
  runMode: RunMode.Normal,

  /**
   * Define CHECKSUM_API_KEY in checksum/.env or your shell.
   * You can find it in https://app.checksum.ai/#/settings/
   */
  apiKey: process.env.CHECKSUM_API_KEY,

  /**
   * Define your test run environments and test users within each environment.
   * The environments must be aligned with those set here:
   * https://app.checksum.ai/#/settings/
   */
  environments: [
    {
      name: "<The name of the environment>",
      // Define BASE_URL in checksum/.env before running your tests.
      baseURL: process.env.BASE_URL!,
      loginURL: "<The URL of the login page>",
      default: true,
      users: [
        {
          role: "<The role of the user, may be undefined in case of single user>",
          username: "<username>",
          password: "<password>",
          default: true,
        },
      ],
    },
  ],

  options: {
    /**
     * Whether to use Checksum Smart Selector in order to recover from failing to locate an element for an action (see README)
     */
    useChecksumSelectors: true,
    /**
     * Whether to use Checksum AI in order to recover from a failed action or assertion (see README)
     */
    useChecksumAI: { actions: true, assertions: false },
    /**
     * Whether to use mock API data when running your tests (see README)
     */
    useMockData: false,
    /**
     * Whether to Upload HTML test reports to app.checksum.ai so they can be viewed through the UI. Only relevant if Playwright reporter config is set to HTML
     * Reports will be saved locally either way (according to Playwright Configs) and can be viewed using the CLI command show-reports.
     */
    hostReports: !!process.env.CI,
    /**
     * Whether to create a PR with healed tests. Only relevant when in Heal mode.
     */
    autoHealPRs: !!process.env.CI,
  },
});
