#!/bin/bash

# Certificate filtering test script
# This script runs certificate filtering tests with various filter combinations

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Change to the project root directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPT_DIR/.." || exit 1
echo "Changed working directory to: $(pwd)"

echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE}    Certificate Filtering Test Runner      ${NC}"
echo -e "${BLUE}============================================${NC}"

# Test data for filtering
TEST_EVENTS=(
  "HVAC System Design Fundamentals"
  "Advanced Refrigeration Technology"
  "Building Automation Systems Workshop"
)

TEST_ATTENDEES=(
  "John Smith"
  "Sarah Johnson"
  "example.com"
)

# Function to run a test with specific filter
run_certificate_test() {
  local test_type=$1
  local filter_value=$2
  local additional_args=$3

  echo -e "\n${YELLOW}Running certificate test with ${test_type} filter: ${filter_value}${NC}"
  
  # Construct test command with proper escaping
  local command="FILTER_TYPE=\"${test_type}\" FILTER_VALUE=\"${filter_value}\" npx playwright test tests/e2e/certificates.test.ts --config=tests/e2e/optimized-playwright.config.ts ${additional_args}"
  
  echo -e "${YELLOW}$ ${command}${NC}"
  
  # Run the test command
  eval "$command"
  
  # Check if test passed
  local exit_code=$?
  if [ $exit_code -eq 0 ]; then
    echo -e "${GREEN}✓ Test passed${NC}"
  else
    echo -e "${RED}✗ Test failed with exit code ${exit_code}${NC}"
  fi
  
  echo -e "${YELLOW}-----------------------------------------------${NC}"
}

# Function to print test summary
print_summary() {
  echo -e "\n${BLUE}============================================${NC}"
  echo -e "${BLUE}    Certificate Filtering Test Summary     ${NC}"
  echo -e "${BLUE}============================================${NC}"
  echo -e "${YELLOW}Filter tests run:${NC} $1"
  echo -e "${GREEN}Tests passed:${NC}    $2"
  echo -e "${RED}Tests failed:${NC}    $3"
  echo -e "${BLUE}============================================${NC}"
}

# Check if the certificates test exists
if [ ! -f "tests/e2e/certificates.test.ts" ]; then
  echo -e "${RED}Error: certificates.test.ts not found!${NC}"
  echo -e "Make sure the certificate test file exists at tests/e2e/certificates.test.ts"
  exit 1
fi

# Check if optimized config exists
if [ ! -f "tests/e2e/optimized-playwright.config.ts" ]; then
  echo -e "${RED}Error: optimized-playwright.config.ts not found!${NC}"
  echo -e "Make sure the optimized configuration file exists at tests/e2e/optimized-playwright.config.ts"
  exit 1
fi

# Process command line arguments
TEST_MODE=1 # Default to run all tests
UI_MODE="--headless" # Default to headless mode
CUSTOM_TYPE=""
CUSTOM_VALUE=""

for arg in "$@"
do
  case $arg in
    --all)
      TEST_MODE=1
      ;;
    --event-only)
      TEST_MODE=2
      ;;
    --attendee-only)
      TEST_MODE=3
      ;;
    --custom=*)
      TEST_MODE=4
      CUSTOM_VALUE="${arg#*=}"
      ;;
    --type=*)
      CUSTOM_TYPE="${arg#*=}"
      ;;
    --headed)
      UI_MODE="--headed"
      ;;
    --help)
      echo -e "${YELLOW}Usage:${NC}"
      echo "  $0 [options]"
      echo ""
      echo "Options:"
      echo "  --all             Run all certificate filter tests (default)"
      echo "  --event-only      Run event filtering tests only"
      echo "  --attendee-only   Run attendee filtering tests only"
      echo "  --custom=VALUE    Run custom filter test with specified value"
      echo "  --type=TYPE       Specify filter type for custom test (event, attendee, combined)"
      echo "  --headed          Run tests with browser UI visible"
      echo "  --help            Show this help message"
      exit 0
      ;;
  esac
done

# Show selected mode
echo -e "\n${YELLOW}Selected test mode:${NC} $TEST_MODE"

# Track test statistics
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

case $TEST_MODE in
  1)
    # Run all test combinations
    echo -e "\n${BLUE}Running all certificate filter tests...${NC}"
    
    # Event filter tests
    for event in "${TEST_EVENTS[@]}"; do
      run_certificate_test "event" "$event" "--headed"
      TOTAL_TESTS=$((TOTAL_TESTS+1))
      [ $? -eq 0 ] && PASSED_TESTS=$((PASSED_TESTS+1)) || FAILED_TESTS=$((FAILED_TESTS+1))
    done
    
    # Attendee filter tests
    for attendee in "${TEST_ATTENDEES[@]}"; do
      run_certificate_test "attendee" "$attendee" "--headed"
      TOTAL_TESTS=$((TOTAL_TESTS+1))
      [ $? -eq 0 ] && PASSED_TESTS=$((PASSED_TESTS+1)) || FAILED_TESTS=$((FAILED_TESTS+1))
    done
    
    # Combined filter test (first event + first attendee)
    run_certificate_test "combined" "${TEST_EVENTS[0]}|${TEST_ATTENDEES[0]}" "--headed"
    TOTAL_TESTS=$((TOTAL_TESTS+1))
    [ $? -eq 0 ] && PASSED_TESTS=$((PASSED_TESTS+1)) || FAILED_TESTS=$((FAILED_TESTS+1))
    ;;
    
  2)
    # Run event filter tests only
    echo -e "\n${BLUE}Running event filter tests...${NC}"
    
    for event in "${TEST_EVENTS[@]}"; do
      run_certificate_test "event" "$event" "--headed"
      TOTAL_TESTS=$((TOTAL_TESTS+1))
      [ $? -eq 0 ] && PASSED_TESTS=$((PASSED_TESTS+1)) || FAILED_TESTS=$((FAILED_TESTS+1))
    done
    ;;
    
  3)
    # Run attendee filter tests only
    echo -e "\n${BLUE}Running attendee filter tests...${NC}"
    
    for attendee in "${TEST_ATTENDEES[@]}"; do
      run_certificate_test "attendee" "$attendee" "--headed"
      TOTAL_TESTS=$((TOTAL_TESTS+1))
      [ $? -eq 0 ] && PASSED_TESTS=$((PASSED_TESTS+1)) || FAILED_TESTS=$((FAILED_TESTS+1))
    done
    ;;
    
  4)
    # Run custom filter test
    echo -e "\n${BLUE}Running custom filter test...${NC}"
    
    read -p "Enter filter type (event, attendee, combined): " CUSTOM_TYPE
    read -p "Enter filter value: " CUSTOM_VALUE
    read -p "Run with UI? (y/n): " WITH_UI
    
    UI_FLAG=""
    [ "$WITH_UI" = "y" ] && UI_FLAG="--headed"
    
    run_certificate_test "$CUSTOM_TYPE" "$CUSTOM_VALUE" "$UI_FLAG"
    TOTAL_TESTS=$((TOTAL_TESTS+1))
    [ $? -eq 0 ] && PASSED_TESTS=$((PASSED_TESTS+1)) || FAILED_TESTS=$((FAILED_TESTS+1))
    ;;
    
  *)
    echo -e "${RED}Invalid option selected.${NC}"
    exit 1
    ;;
esac

# Print test summary
print_summary $TOTAL_TESTS $PASSED_TESTS $FAILED_TESTS

exit 0