import { defineConfig, devices } from '@playwright/test';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const isStaging = process.env.PLAYWRIGHT_STAGING === '1';
const baseURL =
  process.env.PLAYWRIGHT_BASE_URL ??
  (process.env.PLAYWRIGHT_USE_LARAVEL ? 'http://127.0.0.1:8000' : 'http://127.0.0.1:5173');
const authFile = path.join(__dirname, 'playwright/.auth/user.json');

export default defineConfig({
  testDir: './resources/js/src/__tests__/e2e',
  globalSetup: isStaging
    ? './resources/js/src/__tests__/e2e/global-setup.staging.ts'
    : undefined,
  testMatch: process.env.PLAYWRIGHT_FULL_SUITE
    ? ['**/*.spec.ts']
    : isStaging
      ? ['**/staging-verification.spec.ts']
      : [
          '**/critical-flows.spec.ts',
          '**/auth-dashboard-smoke.spec.ts',
          '**/enterprise-agile-flows.spec.ts',
          '**/enterprise-agile-uat.spec.ts',
        ],
  fullyParallel: !isStaging,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: isStaging ? 1 : process.env.CI ? 1 : undefined,
  timeout: isStaging ? 180_000 : 30_000,
  expect: { timeout: isStaging ? 15_000 : 5_000 },
  reporter: process.env.CI
    ? [['github'], ['html', { open: 'never' }], ['json', { outputFile: 'docs/e2e/playwright-report.json' }]]
    : [['html', { open: 'on-failure' }]],
  use: {
    baseURL,
    trace: isStaging ? 'retain-on-failure' : 'on-first-retry',
    screenshot: isStaging ? 'only-on-failure' : 'only-on-failure',
    video: isStaging ? 'retain-on-failure' : 'retain-on-failure',
    actionTimeout: isStaging ? 15_000 : 10_000,
  },

  projects: isStaging
    ? [
        {
          name: 'staging',
          use: { ...devices['Desktop Chrome'] },
        },
      ]
    : [
        {
          name: 'setup',
          testMatch: '**/auth.setup.ts',
        },
        {
          name: 'chromium',
          use: {
            ...devices['Desktop Chrome'],
            storageState: authFile,
          },
          dependencies: ['setup'],
        },
        {
          name: 'screenshots',
          testMatch: '**/ui-screenshot-qa.spec.ts',
          use: {
            ...devices['Desktop Chrome'],
          },
        },
      ],

  webServer: process.env.PLAYWRIGHT_SKIP_WEBSERVER
    ? undefined
    : process.env.PLAYWRIGHT_USE_LARAVEL
      ? [
          {
            command: 'php artisan serve --host=127.0.0.1 --port=8000',
            url: 'http://127.0.0.1:8000',
            reuseExistingServer: !process.env.CI,
            timeout: 180_000,
          },
          {
            command: 'npm run dev -- --host 127.0.0.1 --port 5173',
            url: 'http://127.0.0.1:5173',
            reuseExistingServer: !process.env.CI,
            timeout: 180_000,
          },
        ]
      : {
          command: process.env.CI
            ? 'LARAVEL_BYPASS_ENV_CHECK=1 npm run preview -- --host 127.0.0.1 --port 5173'
            : 'npm run dev -- --host 127.0.0.1 --port 5173',
          url: baseURL,
          reuseExistingServer: !process.env.CI,
          timeout: 180_000,
        },
});
