#!/bin/bash
set -e

# Script to run Playwright tests in parallel with isolated environments
# Each test suite runs against its own backend instance with a separate database

# Parse command line arguments
BROWSERS="realist-chromium,notes-chromium,interface-chromium"  # Default to just chromium for faster testing
HELP=false

# Process command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    --browsers=*)
      BROWSERS="${1#*=}"
      shift
      ;;
    --all-browsers)
      BROWSERS="all"
      shift
      ;;
    --help)
      HELP=true
      shift
      ;;
    *)
      echo "Unknown option: $1"
      HELP=true
      shift
      ;;
  esac
done

# Show help message
if [ "$HELP" = true ]; then
  echo "Usage: $0 [options]"
  echo "Options:"
  echo "  --browsers=LIST    Comma-separated list of browsers to test (chromium,firefox,webkit)"
  echo "  --all-browsers     Test on all browsers (equivalent to --browsers=chromium,firefox,webkit,mobile)"
  echo "  --help             Show this help message"
  exit 0
fi

# Create temporary directory for test databases
TEMP_DIR=$(mktemp -d)
echo "Created temporary directory: $TEMP_DIR"

# Cleanup function to ensure all processes are terminated
cleanup() {
  echo "Cleaning up..."
  # Kill all background processes
  if [ -n "$PID1" ]; then kill $PID1 2>/dev/null || true; fi
  if [ -n "$PID2" ]; then kill $PID2 2>/dev/null || true; fi
  if [ -n "$PID3" ]; then kill $PID3 2>/dev/null || true; fi
  
  # Remove temporary directory
  rm -rf "$TEMP_DIR"
  echo "Cleanup complete"
}

# Set trap to ensure cleanup on exit
trap cleanup EXIT INT TERM

# Build the frontend
echo "Building frontend..."
cd frontend
bun run build
cd ..

# Start backend instances for each test suite
echo "Starting backend instances..."

# Instance 1 for readlist tests (port 3001)
go run main.go -port 3001 -db "$TEMP_DIR/readlist.db" -test &
PID1=$!
echo "Started backend for readlist tests on port 3001 (PID: $PID1)"

# Instance 2 for notes tests (port 3002)
go run main.go -port 3002 -db "$TEMP_DIR/notes.db" -test &
PID2=$!
echo "Started backend for notes tests on port 3002 (PID: $PID2)"

# Instance 3 for interface tests (port 3003)
go run main.go -port 3003 -db "$TEMP_DIR/interface.db" -test &
PID3=$!
echo "Started backend for interface tests on port 3003 (PID: $PID3)"

# Wait for backends to start
echo "Waiting for backends to initialize..."
sleep 5

# Prepare browser arguments for Playwright
BROWSER_ARGS=""
if [ "$BROWSERS" = "all" ]; then
  echo "Running tests on all browsers..."
  # No specific project args means run all projects
else
  echo "Running tests on browsers: $BROWSERS"
  # Convert comma-separated list to space-separated for grep
  BROWSER_LIST=$(echo $BROWSERS | tr ',' ' ')
  
  # Build the project filter
  for BROWSER in $BROWSER_LIST; do
    if [ -n "$BROWSER_ARGS" ]; then
      BROWSER_ARGS="$BROWSER_ARGS --project=.*-$BROWSER"
    else
      BROWSER_ARGS="--project=.*-$BROWSER"
    fi
  done
fi

# Run the tests
echo "Running Playwright tests in parallel..."
cd frontend
npx playwright test --config=playwright.parallel.config.ts $BROWSER_ARGS

# Exit code will be the exit code of the Playwright command
exit $?