import { defineConfig, devices } from '@playwright/test';
import path from 'path';

export default defineConfig({
  testDir: './tests',
  globalSetup: './tests/e2e/global-setup.ts',
  timeout: 45000,
  expect: { timeout: 8000 },

  /* Run tests in files in parallel */
  fullyParallel: true,

  /* Retry once on CI to reduce flakiness from cold-start */
  retries: process.env.CI ? 1 : 0,

  /* Limit concurrency so the dev server isn't overwhelmed */
  workers: process.env.CI ? 2 : 4,

  /* Reporters: dot in CI, list + HTML locally */
  reporter: process.env.CI
    ? [['dot'], ['json', { outputFile: 'test-results/playwright-results.json' }]]
    : [['list'], ['html', { outputFolder: 'playwright-report', open: 'never' }]],

  /* Artifacts directory for screenshots, traces, network logs */
  outputDir: 'tests/artifacts',

  use: {
    baseURL: process.env.PLAYWRIGHT_BASE_URL || 'http://skinbase26.test',
    headless: true,
    viewport: { width: 1280, height: 720 },
    ignoreHTTPSErrors: true,

    /* Capture trace, screenshot and video on failure */
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
  },

  projects: [
    /* Setup project: logs in and saves session storage */
    {
      name: 'cpad-setup',
      testMatch: /cpad\/.*\.setup\.ts/,
      use: { ...devices['Desktop Chrome'] },
    },
    /* cPad control-panel tests — reuse authenticated session */
    {
      name: 'cpad',
      testDir: './tests/cpad',
      use: {
        ...devices['Desktop Chrome'],
        storageState: path.join('tests', '.auth', 'admin.json'),
      },
      dependencies: ['cpad-setup'],
    },
    /* General e2e suite */
    {
      name: 'chromium',
      testDir: './tests/e2e',
      use: { ...devices['Desktop Chrome'] },
    },
    /* Uncomment to add Firefox/WebKit coverage:
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
    */
  ],
});
