/**
 * Playwright Configuration for HVAC Plugin Comprehensive Tests
 * 
 * Configuration for all HVAC plugin test suites including:
 * - CSS Asset Loading Tests
 * - Authentication System Tests  
 * - AJAX Security Tests
 * - Bundled Assets Tests
 * - E2E Functionality Tests
 * 
 * @package HVAC_Community_Events
 * @since 2.0.0
 */

const { defineConfig, devices } = require('@playwright/test');

// Environment configuration
const isGnomeDesktop = process.env.XDG_CURRENT_DESKTOP === 'GNOME';
const hasDisplay = process.env.DISPLAY || process.env.WAYLAND_DISPLAY;
const useHeaded = !process.env.CI && process.env.HEADLESS !== 'true' && isGnomeDesktop && hasDisplay;

// Base URL configuration
const baseUrl = process.env.BASE_URL || 'http://localhost:8080';

module.exports = defineConfig({
  // Test discovery
  testDir: './',
  testMatch: [
    '**/css-asset-loading.test.js',
    '**/authentication-system.test.js',
    '**/ajax-security.test.js',
    '**/bundled-assets.test.js',
    '**/bundled-assets-standalone.test.js',
    '**/build-system-security.test.js',
    '**/e2e-bundled-assets-functionality.test.js'
  ],
  
  // Execution settings
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 1,
  workers: process.env.CI ? 1 : 2,
  
  // Timeout settings
  timeout: 60000, // 1 minute per test
  expect: {
    timeout: 10000 // 10 seconds for assertions
  },
  
  // Global setup and teardown
  // globalSetup: require.resolve('./setup/global-setup.js'),
  // globalTeardown: require.resolve('./setup/global-teardown.js'),
  
  // Output and reporting
  outputDir: './test-results/screenshots',
  reporter: [
    ['html', { 
      outputFolder: './test-results/html-report',
      open: 'never'
    }],
    ['json', { 
      outputFile: './test-results/test-results.json' 
    }],
    ['list'],
    ['github'] // For CI
  ],
  
  // Global test configuration
  use: {
    // Base URL for all tests
    baseURL: baseUrl,
    
    // Browser context options
    headless: !useHeaded,
    slowMo: useHeaded ? 500 : 0,
    
    // Viewport and device emulation
    viewport: { width: 1280, height: 720 },
    
    // Screenshots and videos
    screenshot: 'only-on-failure',
    video: process.env.CI ? 'retain-on-failure' : 'off',
    trace: 'retain-on-failure',
    
    // Navigation and timing
    navigationTimeout: 30000,
    actionTimeout: 10000,
    
    // Browser launch options for headed testing
    ...(useHeaded && {
      launchOptions: {
        args: [
          '--start-maximized',
          '--disable-features=TranslateUI'
        ],
        slowMo: 500
      }
    })
  },
  
  // Browser projects
  projects: [
    {
      name: 'chromium',
      use: { 
        ...devices['Desktop Chrome'],
        channel: 'chrome'
      },
    },
    
    // Additional browsers for comprehensive testing
    {
      name: 'firefox',
      use: { 
        ...devices['Desktop Firefox'] 
      },
    },
    
    {
      name: 'webkit',
      use: { 
        ...devices['Desktop Safari'] 
      },
    },
    
    // Mobile testing
    {
      name: 'Mobile Chrome',
      use: { 
        ...devices['Pixel 5'] 
      },
    },
    
    {
      name: 'Mobile Safari',
      use: { 
        ...devices['iPhone 12'] 
      },
    }
  ],
  
  // Web server configuration disabled - using external server
  // webServer: undefined,
});

console.log('🧪 Playwright Configuration Loaded');
console.log(`📍 Base URL: ${baseUrl}`);
console.log(`🖥️  Headed Mode: ${useHeaded ? 'ON' : 'OFF'}`);
console.log(`🔧 Environment: ${process.env.NODE_ENV || 'development'}`);
if (useHeaded) {
  console.log(`📺 Display: ${process.env.DISPLAY || process.env.WAYLAND_DISPLAY}`);
  console.log(`🖼️  Desktop: ${process.env.XDG_CURRENT_DESKTOP}`);
}
console.log('');