import { defineConfig, devices } from '@playwright/test'
import dotenv from 'dotenv'
import { fileURLToPath } from 'url'
import { dirname, resolve } from 'path'

// Get __dirname equivalent in ES modules
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

// Load test environment variables
// In CI, don't load .env.test to allow GitHub Actions environment variables to be used
// Locally, load .env.test to configure test database and SMTP
if (!process.env.CI) {
  dotenv.config({
    path: resolve(__dirname, '.env.test'),
    override: true,
    // Suppress dotenv promotional messages
    quiet: true
  })
}

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: './test/e2e',

  /* 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 ? 1 : 0,

  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 1 : undefined,

  /* Global timeout for entire test run (30 minutes) */
  globalTimeout: process.env.CI ? 30 * 60 * 1000 : undefined,

  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: process.env.CI ? 'github' : 'list',

  /* 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:3000',

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry',

    /* Screenshot on failure */
    screenshot: 'only-on-failure',

    /* Video on failure */
    video: 'retain-on-failure',

    /* Run in headless mode */
    headless: true,

    /* Default timeout for actions (30 seconds) */
    actionTimeout: 30000,

    /* Default navigation timeout (30 seconds) */
    navigationTimeout: 30000
  },

  /* Global timeout for each test */
  timeout: process.env.CI ? 60000 : 120000, // 1 min in CI, 2 min locally

  /* Timeout for expect() assertions */
  expect: {
    timeout: process.env.CI ? 5000 : 10000 // 5 sec in CI, 10 sec locally
  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        // Set Dutch locale to match application default
        locale: 'nl-BE'
      }
    }

    // Uncomment to test on other browsers
    // {
    //   name: 'firefox',
    //   use: { ...devices['Desktop Firefox'] },
    // },

    // {
    //   name: 'webkit',
    //   use: { ...devices['Desktop Safari'] },
    // },

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

  /* Run your local dev server before starting the tests */
  webServer: {
    // Start Nuxt dev server with test environment variables
    // Use wrapper script to ensure .env.test is loaded and server is ready
    command: 'bash scripts/start-test-server.sh',
    // Script handles health check internally, so just set port for Playwright
    port: 3000,
    reuseExistingServer: !process.env.CI,
    timeout: 180 * 1000, // 3 minutes to allow for script's internal polling
    stdout: 'pipe',
    stderr: 'pipe'
  }
})
