#!/bin/bash
# =====================================================================
# run-tests.sh - Main test runner for the resume site
# =====================================================================
# This script starts a local Caddy server and runs all tests against it
# =====================================================================

set -e  # Exit on any error

# Define constants
TEST_PORT=8080
CADDY_DIR="docker/resume"
CADDY_FILE="Caddyfile.local"
TESTS_DIR="$(dirname "$0")"
LOG_FILE="$TESTS_DIR/test-run.log"

# Function to clean up processes on exit
cleanup() {
  echo "Cleaning up..."
  # Find and kill any Caddy processes we started
  if [ -f "$TESTS_DIR/.caddy.pid" ]; then
    CADDY_PID=$(cat "$TESTS_DIR/.caddy.pid")
    if ps -p $CADDY_PID > /dev/null; then
      echo "Stopping Caddy server (PID: $CADDY_PID)"
      kill $CADDY_PID
    fi
    rm "$TESTS_DIR/.caddy.pid"
  fi
  
  echo "Cleanup complete"
}

# Register the cleanup function to run on exit
trap cleanup EXIT

# Start the Caddy server
start_caddy() {
  echo "Starting Caddy server on port $TEST_PORT..."
  
  # Navigate to the Caddy directory
  cd "$CADDY_DIR"
  
  # Check if Caddyfile.local exists, if not, create it from Caddyfile
  if [ ! -f "$CADDY_FILE" ]; then
    echo "Creating $CADDY_FILE from Caddyfile..."
    cp Caddyfile "$CADDY_FILE"
    # Modify the Caddyfile.local to use the test port
    sed -i '' "s/:80/:$TEST_PORT/g" "$CADDY_FILE"
  fi
  
  # Start Caddy in the background using the local config
  echo "Running: caddy run --config $CADDY_FILE"
  mkdir -p $(dirname "$LOG_FILE") && caddy run --config "$CADDY_FILE" > "$LOG_FILE" 2>&1 &
  CADDY_PID=$!
  mkdir -p "$TESTS_DIR" && echo $CADDY_PID > "$TESTS_DIR/.caddy.pid"
  
  # Return to the original directory
  cd - > /dev/null
  
  # Wait for Caddy to start
  echo "Waiting for Caddy to start..."
  sleep 2
  
  # Check if Caddy is running
  if ! ps -p $CADDY_PID > /dev/null; then
    echo "Failed to start Caddy server. Check $LOG_FILE for details."
    exit 1
  fi
  
  echo "Caddy server started with PID: $CADDY_PID"
  
  # Wait a bit more to ensure Caddy is fully initialized
  sleep 3
}

# Run Node.js server for tests that require it
start_node_server() {
  if [ -f "$TESTS_DIR/serve.js" ]; then
    echo "Starting Node.js server for tests..."
    node "$TESTS_DIR/serve.js" > "$TESTS_DIR/node-server.log" 2>&1 &
    NODE_SERVER_PID=$!
    echo $NODE_SERVER_PID > "$TESTS_DIR/.node-server.pid"
    
    # Wait for Node.js server to start
    sleep 2
    
    # Check if Node.js server is running
    if ! ps -p $NODE_SERVER_PID > /dev/null; then
      echo "Failed to start Node.js server. Check $TESTS_DIR/node-server.log for details."
      exit 1
    fi
    
    echo "Node.js server started with PID: $NODE_SERVER_PID"
  fi
}

# Clean up Node.js server
cleanup_node_server() {
  if [ -f "$TESTS_DIR/.node-server.pid" ]; then
    NODE_SERVER_PID=$(cat "$TESTS_DIR/.node-server.pid")
    if ps -p $NODE_SERVER_PID > /dev/null; then
      echo "Stopping Node.js server (PID: $NODE_SERVER_PID)"
      kill $NODE_SERVER_PID
    fi
    rm "$TESTS_DIR/.node-server.pid"
  fi
}

# Run shell script tests
run_shell_tests() {
  echo "Running shell script tests..."
  
  # Run unit tests
  echo "Running unit tests..."
  if [ -d "$TESTS_DIR/unit" ] && [ "$(ls -A "$TESTS_DIR/unit")" ]; then
    for test in "$TESTS_DIR/unit"/*.sh; do
      if [ -f "$test" ] && [ -x "$test" ]; then
        echo "Running unit test: $(basename "$test")"
        "$test" || echo "FAILED: $(basename "$test")"
      fi
    done
  else
    echo "No unit tests found."
  fi
  
  # Run integration tests
  echo "Running integration tests..."
  if [ -d "$TESTS_DIR/integration" ] && [ "$(ls -A "$TESTS_DIR/integration")" ]; then
    for test in "$TESTS_DIR/integration"/*.sh; do
      if [ -f "$test" ] && [ -x "$test" ]; then
        echo "Running integration test: $(basename "$test")"
        "$test" "http://localhost:$TEST_PORT" || echo "FAILED: $(basename "$test")"
      fi
    done
    
    # Check for subdirectories
    for dir in "$TESTS_DIR/integration"/*/; do
      if [ -d "$dir" ]; then
        for test in "$dir"*.sh; do
          if [ -f "$test" ] && [ -x "$test" ]; then
            echo "Running integration test: $(basename "$test")"
            "$test" "http://localhost:$TEST_PORT" || echo "FAILED: $(basename "$test")"
          fi
        done
      fi
    done
  else
    echo "No integration tests found."
  fi
  
  # Run e2e tests
  echo "Running e2e tests..."
  if [ -d "$TESTS_DIR/e2e" ] && [ "$(ls -A "$TESTS_DIR/e2e")" ]; then
    for test in "$TESTS_DIR/e2e"/*.sh; do
      if [ -f "$test" ] && [ -x "$test" ]; then
        echo "Running e2e test: $(basename "$test")"
        "$test" "http://localhost:$TEST_PORT" || echo "FAILED: $(basename "$test")"
      fi
    done
  else
    echo "No e2e tests found."
  fi
}

# Run JavaScript tests
run_js_tests() {
  echo "Running JavaScript tests..."
  
  # Check if Playwright is installed
  if command -v npx &> /dev/null && npx playwright --version &> /dev/null; then
    # Run Playwright tests
    echo "Running Playwright tests..."
    npx playwright test || echo "FAILED: Playwright tests"
  else
    echo "Playwright not found, skipping Playwright tests."
  fi
  
  # Run Lighthouse tests if available
  if [ -f "$TESTS_DIR/lighthouse.js" ]; then
    echo "Running Lighthouse tests..."
    node "$TESTS_DIR/lighthouse.js" "http://localhost:$TEST_PORT" || echo "FAILED: Lighthouse tests"
  fi
}

# Main execution
echo "=== Starting Test Suite ==="
start_caddy
start_node_server
run_shell_tests
run_js_tests
cleanup_node_server
echo "=== Test Suite Completed ==="
