#!/bin/bash

# E2E Testing Suite Optimization Script
# This script helps troubleshoot and optimize the Playwright E2E testing configuration

echo "===== HVAC Community Events E2E Testing Suite Optimization ====="
echo

# Step 1: Check for multiple Playwright installations
echo "Step 1: Checking for duplicate Playwright installations..."
PLAYWRIGHT_GLOBAL=$(npm list -g playwright | grep playwright | wc -l)
PLAYWRIGHT_LOCAL_DEP=$(npm list playwright | grep playwright | wc -l)
PLAYWRIGHT_TEST_DEP=$(npm list @playwright/test | grep @playwright/test | wc -l)

echo "Playwright global installations: $PLAYWRIGHT_GLOBAL"
echo "Playwright local installations: $PLAYWRIGHT_LOCAL_DEP"
echo "@playwright/test installations: $PLAYWRIGHT_TEST_DEP"

if [ $PLAYWRIGHT_LOCAL_DEP -gt 1 ] || [ $PLAYWRIGHT_TEST_DEP -gt 1 ]; then
    echo "⚠️  Detected multiple Playwright instances. This may cause conflicts."
    echo "    Consider fixing with: npm uninstall playwright && npm install playwright@latest"
else
    echo "✅ Playwright installation looks good."
fi

echo

# Step 2: Check for proper directory structure
echo "Step 2: Verifying E2E test directory structure..."
UTILS_DIR="./tests/e2e/utils"
PAGES_DIR="./tests/e2e/pages"
CONFIG_FILE="./playwright.config.ts"

if [ -d "$UTILS_DIR" ] && [ -d "$PAGES_DIR" ] && [ -f "$CONFIG_FILE" ]; then
    echo "✅ Basic directory structure is in place."
else
    echo "❌ Missing critical directories or files:"
    [ ! -d "$UTILS_DIR" ] && echo "   - Missing $UTILS_DIR"
    [ ! -d "$PAGES_DIR" ] && echo "   - Missing $PAGES_DIR"
    [ ! -f "$CONFIG_FILE" ] && echo "   - Missing $CONFIG_FILE"
fi

echo

# Step 3: Check dependencies in package.json
echo "Step 3: Checking required dependencies..."
MISSING_DEPS=0

check_dependency() {
    local DEP=$1
    local COUNT=$(npm list $DEP 2>/dev/null | grep $DEP | wc -l)
    if [ $COUNT -eq 0 ]; then
        echo "❌ Missing dependency: $DEP"
        MISSING_DEPS=$((MISSING_DEPS + 1))
    else
        local VERSION=$(npm list $DEP | grep $DEP)
        echo "✅ Found $VERSION"
    fi
}

check_dependency "@playwright/test"
check_dependency "playwright"
check_dependency "dotenv"
check_dependency "typescript"

if [ $MISSING_DEPS -gt 0 ]; then
    echo
    echo "⚠️  Some dependencies are missing. Consider running:"
    echo "    npm install @playwright/test playwright dotenv typescript --save-dev"
else
    echo "✅ All required dependencies are installed."
fi

echo

# Step 4: Check Playwright configuration
echo "Step 4: Validating Playwright configuration..."
if [ -f "$CONFIG_FILE" ]; then
    CONFIG_ISSUES=0
    
    # Check for testDir setting
    if grep -q "testDir:" "$CONFIG_FILE"; then
        echo "✅ testDir setting found in configuration"
    else
        echo "❌ Missing 'testDir' setting in Playwright config"
        CONFIG_ISSUES=$((CONFIG_ISSUES + 1))
    fi
    
    # Check for projects setting
    if grep -q "projects:" "$CONFIG_FILE"; then
        echo "✅ projects setting found in configuration"
    else
        echo "❌ Missing 'projects' setting in Playwright config"
        CONFIG_ISSUES=$((CONFIG_ISSUES + 1))
    fi
    
    # Check for use setting
    if grep -q "use:" "$CONFIG_FILE"; then
        echo "✅ use setting found in configuration"
    else
        echo "❌ Missing 'use' setting in Playwright config"
        CONFIG_ISSUES=$((CONFIG_ISSUES + 1))
    fi
    
    if [ $CONFIG_ISSUES -gt 0 ]; then
        echo
        echo "⚠️  There are issues with your Playwright configuration."
        echo "   Review the playwright.config.ts file and fix the noted issues."
    else
        echo "✅ Basic Playwright configuration looks good."
    fi
else
    echo "❌ Playwright configuration file not found."
fi

echo

# Step 5: Test an optimized certificate test
echo "Step 5: Setting up and testing optimized certificate test..."

if [ -f "./tests/e2e/optimized-certificate-tests.ts" ]; then
    echo "✅ Optimized certificate test found."
    echo "   Running npx playwright test tests/e2e/optimized-certificate-tests.ts --headed --workers=1"
    
    # Uncomment to actually run the test
    # npx playwright test tests/e2e/optimized-certificate-tests.ts --headed --workers=1
    
    echo "⚠️  Test execution commented out. Uncomment in script to run actual test."
else
    echo "❌ Optimized certificate test not found."
    echo "   Make sure optimized-certificate-tests.ts exists in tests/e2e directory."
fi

echo

# Step 6: Create improved configuration
echo "Step 6: Creating improved Playwright configuration..."

cat > playwright.config.optimized.ts << 'EOF'
import { defineConfig, devices } from '@playwright/test';
import path from 'path';
import dotenv from 'dotenv';

// Load environment variables
dotenv.config();

// Base URL for all tests
const BASE_URL = process.env.BASE_URL || 'https://wordpress-974670-5399585.cloudwaysapps.com';

/**
 * Optimized Playwright configuration
 * - Simplified configuration focusing on reliability
 * - Reduced dependencies on external utilities
 * - Streamlined reporter configuration
 */
export default defineConfig({
  // Basic test configuration
  testDir: './tests/e2e',
  timeout: 45000, // Increased timeout for WordPress operations
  fullyParallel: false, // Disable parallel tests for WordPress to prevent conflicts
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 1, // Add one retry even in development
  workers: 1, // Limit to one worker for WordPress tests
  
  // Simple reporter configuration
  reporter: [
    ['list'], // More informative than dot reporter
    ['html', { open: 'never' }], // HTML report for better visualization
  ],

  // Global test configuration
  use: {
    baseURL: BASE_URL,
    trace: 'retain-on-failure',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    
    // Browser context options
    viewport: { width: 1280, height: 720 },
    ignoreHTTPSErrors: true, // Needed for many WordPress staging sites
    
    // Actionability configuration
    actionTimeout: 15000, // Increase timeout for WordPress slow UI
  },

  // Test projects - minimal configuration for stability
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
  
  // Global setup/teardown - minimal stubs
  globalSetup: './tests/e2e/global-setup.js',
  globalTeardown: './tests/e2e/global-teardown.js',
});
EOF

echo "✅ Created optimized Playwright configuration: playwright.config.optimized.ts"

echo

# Step 7: Recommendations
echo "Step 7: Recommendations for E2E test suite optimization"
echo
echo "Based on the analysis, here are key recommendations:"
echo
echo "1. Package and Dependency Management:"
echo "   - ✅ Use a single version of Playwright and @playwright/test"
echo "   - ✅ Pin dependency versions in package.json"
echo "   - ✅ Consider using npm-check-updates to safely update dependencies"
echo
echo "2. Configuration Structure:"
echo "   - ✅ Use the optimized configuration file created by this script"
echo "   - ✅ Reduce dependencies on complex utility functions"
echo "   - ✅ Simplify test setup with direct configuration"
echo
echo "3. Test Organization:"
echo "   - ✅ Focus on writing independent, self-contained tests"
echo "   - ✅ Use the Page Object Model pattern consistently"
echo "   - ✅ Keep selectors centralized in Page classes"
echo
echo "4. Troubleshooting Common Issues:"
echo "   - ✅ Test describe() and use() conflicts in imports"
echo "   - ✅ Check for multiple versions of Playwright"
echo "   - ✅ Use --headed and --slow flags for debugging"
echo "   - ✅ Add page.pause() for interactive debugging"
echo
echo "5. Certificate Testing Specific:"
echo "   - ✅ Use the optimized certificate test as a template"
echo "   - ✅ Focus on testing one feature at a time"
echo "   - ✅ Add proper screenshots for validation"
echo 
echo "To apply the optimized configuration:"
echo "  cp playwright.config.optimized.ts playwright.config.ts"
echo
echo "To run certificate tests with the optimized config:"
echo "  npx playwright test tests/e2e/optimized-certificate-tests.ts --config=playwright.config.optimized.ts --headed"

echo
echo "===== E2E TESTING SUITE OPTIMIZATION COMPLETE ====="