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

/**
 * Comprehensive E2E Testing Configuration for mockupAWS v1.0.0
 * 
 * Features:
 * - Multi-browser testing (Chrome, Firefox, Safari)
 * - Mobile testing (iOS, Android)
 * - Parallel execution
 * - Visual regression
 * - 80%+ feature coverage
 */

export default defineConfig({
  // Test directory
  testDir: './e2e-v100',

  // Run tests in parallel for faster execution
  fullyParallel: true,

  // Fail the build on CI if test.only is left in source
  forbidOnly: !!process.env.CI,

  // Retry configuration for flaky tests
  retries: process.env.CI ? 2 : 1,

  // Workers configuration
  workers: process.env.CI ? 4 : undefined,

  // Reporter configuration
  reporter: [
    ['html', { outputFolder: 'e2e-v100-report', open: 'never' }],
    ['list'],
    ['junit', { outputFile: 'e2e-v100-report/results.xml' }],
    ['json', { outputFile: 'e2e-v100-report/results.json' }],
  ],

  // Global timeout
  timeout: 120000,

  // Expect timeout
  expect: {
    timeout: 15000,
  },

  // Shared settings
  use: {
    // Base URL
    baseURL: process.env.TEST_BASE_URL || 'http://localhost:5173',

    // Trace on first retry
    trace: 'on-first-retry',

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

    // Video on first retry
    video: 'on-first-retry',

    // Action timeout
    actionTimeout: 15000,

    // Navigation timeout
    navigationTimeout: 30000,

    // Viewport
    viewport: { width: 1280, height: 720 },

    // Ignore HTTPS errors (for local development)
    ignoreHTTPSErrors: true,
  },

  // Configure projects for different browsers and viewports
  projects: [
    // ============================================
    // DESKTOP BROWSERS
    // ============================================
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },

    // ============================================
    // MOBILE BROWSERS
    // ============================================
    {
      name: 'Mobile Chrome',
      use: { ...devices['Pixel 5'] },
    },
    {
      name: 'Mobile Safari',
      use: { ...devices['iPhone 12'] },
    },
    {
      name: 'Tablet Chrome',
      use: { ...devices['iPad Pro 11'] },
    },
    {
      name: 'Tablet Safari',
      use: { ...devices['iPad (gen 7)'] },
    },

    // ============================================
    // VISUAL REGRESSION BASELINE
    // ============================================
    {
      name: 'visual-regression',
      use: { 
        ...devices['Desktop Chrome'],
        viewport: { width: 1280, height: 720 },
      },
      testMatch: /.*\.visual\.spec\.ts/,
    },
  ],

  // Web server configuration
  webServer: {
    command: 'npm run dev',
    url: 'http://localhost:5173',
    reuseExistingServer: !process.env.CI,
    timeout: 120 * 1000,
    stdout: 'pipe',
    stderr: 'pipe',
  },

  // Output directory
  outputDir: 'e2e-v100-results',

  // Global setup and teardown
  globalSetup: './e2e-v100/global-setup.ts',
  globalTeardown: './e2e-v100/global-teardown.ts',

  // Test match patterns
  testMatch: [
    '**/*.spec.ts',
    '!**/*.visual.spec.ts',  // Exclude visual tests from default run
  ],
});
