#!/bin/bash

# KiviCare API Test Runner
# Runs different types of tests with proper configuration

set -e

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$PROJECT_ROOT"

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

echo -e "${BLUE}🧪 KiviCare API Test Runner${NC}"
echo "=================================="

# Function to run a specific test suite
run_test_suite() {
    local suite=$1
    local description=$2
    
    echo -e "\n${YELLOW}📋 Running $description...${NC}"
    
    if [ "$suite" = "all" ]; then
        php vendor/bin/phpunit
    else
        php vendor/bin/phpunit --testsuite="$suite"
    fi
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✅ $description passed!${NC}"
    else
        echo -e "${RED}❌ $description failed!${NC}"
        return 1
    fi
}

# Parse command line arguments
SUITE="all"
COVERAGE=false

while [[ $# -gt 0 ]]; do
    case $1 in
        -s|--suite)
            SUITE="$2"
            shift 2
            ;;
        -c|--coverage)
            COVERAGE=true
            shift
            ;;
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  -s, --suite SUITE    Run specific test suite (unit|integration|contract|performance|all)"
            echo "  -c, --coverage       Generate code coverage report"
            echo "  -h, --help           Show this help message"
            echo ""
            echo "Available test suites:"
            echo "  unit         - Unit tests only"
            echo "  integration  - Integration tests only" 
            echo "  contract     - API contract tests only"
            echo "  performance  - Performance tests only"
            echo "  all          - All test suites (default)"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use -h for help"
            exit 1
            ;;
    esac
done

# Check if WordPress test environment is set up
if [ ! -d "/tmp/wordpress-tests-lib" ]; then
    echo -e "${YELLOW}⚠️  WordPress test environment not found${NC}"
    echo -e "${YELLOW}Please run: bin/install-wp-tests.sh wordpress_test root '' localhost latest${NC}"
    echo ""
    read -p "Do you want to set up the test environment now? [y/N]: " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo -e "${BLUE}🔧 Setting up WordPress test environment...${NC}"
        bin/install-wp-tests.sh wordpress_test root '' localhost latest
    else
        echo -e "${RED}❌ Cannot run tests without WordPress test environment${NC}"
        exit 1
    fi
fi

# Ensure output directories exist
mkdir -p tests/_output
mkdir -p coverage-html

# Run the specified test suite
case $SUITE in
    "unit")
        run_test_suite "KiviCare API Unit Tests" "Unit Tests"
        ;;
    "integration") 
        run_test_suite "KiviCare API Integration Tests" "Integration Tests"
        ;;
    "contract")
        run_test_suite "KiviCare API Contract Tests" "Contract Tests"
        ;;
    "performance")
        run_test_suite "KiviCare API Performance Tests" "Performance Tests"
        ;;
    "all")
        echo -e "${BLUE}🚀 Running all test suites...${NC}"
        
        run_test_suite "KiviCare API Unit Tests" "Unit Tests"
        run_test_suite "KiviCare API Integration Tests" "Integration Tests" 
        run_test_suite "KiviCare API Contract Tests" "Contract Tests"
        run_test_suite "KiviCare API Performance Tests" "Performance Tests"
        ;;
    *)
        echo -e "${RED}❌ Unknown test suite: $SUITE${NC}"
        echo "Use -h for help"
        exit 1
        ;;
esac

# Generate coverage if requested
if [ "$COVERAGE" = true ]; then
    echo -e "\n${BLUE}📊 Generating code coverage report...${NC}"
    php vendor/bin/phpunit --coverage-html coverage-html
    echo -e "${GREEN}✅ Coverage report generated in coverage-html/${NC}"
fi

echo -e "\n${GREEN}🎉 Testing completed successfully!${NC}"