import { defineConfig, devices } from "@playwright/test";
import { isLinuxEnvironment, getOSSpecificConfig } from './test-playwright/testUtils';

/**
 * Read environment variables from file.
 * https://github.com/motdotla/dotenv
 */
// import dotenv from 'dotenv';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: "./test-playwright",
  ...getOSSpecificConfig(),
  /* Run tests in files in parallel */
  fullyParallel: true,
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests on CI. */
  workers: isLinuxEnvironment() ? 4 : undefined,
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: [
    ['list'],
    ['html'],
    // ['html', { open: 'never' }], // don't automatically open report page after tests complete even with failures
    ['json', { outputFile: 'test-playwright-results/test-results.json' }]
  ],
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {
    /* Base URL to use in actions like `await page.goto('/')`. */
    baseURL: "http://localhost:8081",

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: "retain-on-failure",

    // Add request logging
    logger: {
      isEnabled: (name, severity) => severity === 'error' || name === 'api',
      log: (name, severity, message, args) => console.log(`${severity}: ${message}`, args)
    }
  },

  /* Configure projects for major browsers */
  projects: [
    // {
    //   name: 'chromium-serial',
    //   testMatch: /.*\/(35-record-gift-from-image-share|40-add-contact)\.spec\.ts/,
    //   use: {
    //     ...devices['Desktop Chrome'],
    //     permissions: ["clipboard-read"],
    //   },
    //   workers: 1, // Force serial execution for problematic tests
    // },
    // {
    //   name: 'firefox-serial',
    //   testMatch: /.*\/(35-record-gift-from-image-share|40-add-contact)\.spec\.ts/,
    //   use: { ...devices['Desktop Firefox'] },
    //   workers: 1,
    // },
    {
      name: 'chromium',
      testMatch: /^(?!.*\/(35-record-gift-from-image-share|40-add-contact)\.spec\.ts).+\.spec\.ts$/,
      use: {
        ...devices['Desktop Chrome'],
        permissions: ["clipboard-read"],
      },
    },
    {
      name: 'firefox',
      testMatch: /^(?!.*\/(35-record-gift-from-image-share|40-add-contact)\.spec\.ts).+\.spec\.ts$/,
      use: { ...devices['Desktop Firefox'] },
    },

    /* Test against mobile viewports. */
    // {
    //   name: "Mobile Chrome",
    //   use: { ...devices["Pixel 5"] },
    // },
    // {
    //   name: "Mobile Safari",
    //   use: { ...devices["iPhone 12"] },
    // },

    /* Test against branded browsers. */
    // {
    //   name: 'Microsoft Edge',
    //   use: { ...devices['Desktop Edge'], channel: 'msedge' },
    // },
  ],

  /* Configure global timeout; default is 30000 milliseconds */
  // the image upload will often not succeed in 5 seconds
  // 33-record-gift-x10.spec.ts:90:5 > Record 9 new gifts will often not succeed in 30 seconds
  timeout: 45000, // various tests fail at various times with 25000

  /* Run your local dev server before starting the tests */
  /**
   * This could be an array of servers, meaning we could start the Endorser server as well:
   *     {
   *       command: "cd ../endorser-ch; NODE_ENV=test-local npm run dev",
   *       url: 'http://localhost:3000',
   *       reuseExistingServer: !process.env.CI,
   *     },
   *
   * But if we do then the testInfo.config.webServer is null and the API-setting test 00 fails.
   * It is worth considering a change such that Time Safari's default Endorser API server is NOT set
   * in the user's settings so that it can be blanked out and the default is used.
   */
  webServer: {
    command:
      "VITE_APP_SERVER=http://localhost:8081 VITE_DEFAULT_ENDORSER_API_SERVER=http://localhost:3000 VITE_DEFAULT_PARTNER_API_SERVER=http://localhost:3000 VITE_DEFAULT_IMAGE_API_SERVER=https://test-image-api.timesafari.app VITE_PASSKEYS_ENABLED=true npm run dev -- --port=8081",
    url: "http://localhost:8081",
    reuseExistingServer: !process.env.CI,
  },
});
