#!/bin/bash

# Shown Connector Test Runner
# This script provides a simple way to run tests for the Shown Connector plugin

set -e

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

# Default values
DOCKER_MODE=true
VERBOSE=false
COVERAGE=false
SPECIFIC_TEST=""

# Function to display usage
usage() {
    echo "Usage: $0 [OPTIONS] [TEST_FILE]"
    echo ""
    echo "OPTIONS:"
    echo "  -h, --help       Show this help message"
    echo "  -v, --verbose    Run tests in verbose mode"
    echo "  -c, --coverage   Run tests with coverage report"
    echo "  -s, --standalone Run tests in standalone mode (not in Docker)"
    echo "  --debug          Run tests with debug output"
    echo ""
    echo "EXAMPLES:"
    echo "  $0                           # Run all tests"
    echo "  $0 -v                        # Run all tests with verbose output"
    echo "  $0 -c                        # Run all tests with coverage"
    echo "  $0 tests/test-shown-api.php  # Run specific test file"
    echo "  $0 -s                        # Run tests in standalone mode"
    exit 1
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            usage
            ;;
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        -c|--coverage)
            COVERAGE=true
            shift
            ;;
        -s|--standalone)
            DOCKER_MODE=false
            shift
            ;;
        --debug)
            VERBOSE=true
            DEBUG=true
            shift
            ;;
        tests/*)
            SPECIFIC_TEST=$1
            shift
            ;;
        *)
            echo -e "${RED}Unknown option: $1${NC}"
            usage
            ;;
    esac
done

# Check if Docker is available and running
check_docker() {
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}Docker is not installed or not in PATH${NC}"
        return 1
    fi
    
    if ! docker info &> /dev/null; then
        echo -e "${RED}Docker daemon is not running${NC}"
        return 1
    fi
    
    return 0
}

# Check if WordPress test suite is installed
check_wp_tests() {
    if [ ! -d "/tmp/wordpress-tests-lib" ]; then
        echo -e "${YELLOW}WordPress test suite not found. Please run: make install_wp_tests${NC}"
        return 1
    fi
    return 0
}

# Run tests in Docker
run_docker_tests() {
    echo -e "${GREEN}Running tests in Docker...${NC}"
    
    if ! check_docker; then
        echo -e "${RED}Docker is not available. Try running with -s for standalone mode${NC}"
        exit 1
    fi
    
    # Build phpunit command
    PHPUNIT_CMD="vendor/bin/phpunit"
    
    if [ "$VERBOSE" = true ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD --verbose"
    fi
    
    if [ "$DEBUG" = true ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD --debug"
    fi
    
    if [ "$COVERAGE" = true ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD --coverage-text"
    fi
    
    if [ ! -z "$SPECIFIC_TEST" ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD $SPECIFIC_TEST"
    fi
    
    # Run the tests
    docker-compose exec wordpress bash -c "cd /var/www/html/wp-content/plugins/shown-connector && WP_TESTS_DIR=/tmp/wordpress-tests-lib WP_CORE_DIR=/tmp/wordpress $PHPUNIT_CMD"
}

# Run tests in standalone mode
run_standalone_tests() {
    echo -e "${GREEN}Running tests in standalone mode...${NC}"
    
    if ! check_wp_tests; then
        echo -e "${RED}Please set up WordPress test suite first${NC}"
        exit 1
    fi
    
    # Build phpunit command
    PHPUNIT_CMD="vendor/bin/phpunit"
    
    if [ "$VERBOSE" = true ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD --verbose"
    fi
    
    if [ "$DEBUG" = true ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD --debug"
    fi
    
    if [ "$COVERAGE" = true ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD --coverage-text"
    fi
    
    if [ ! -z "$SPECIFIC_TEST" ]; then
        PHPUNIT_CMD="$PHPUNIT_CMD $SPECIFIC_TEST"
    fi
    
    # Set environment variables and run tests
    export WP_TESTS_DIR=/tmp/wordpress-tests-lib
    export WP_CORE_DIR=/tmp/wordpress
    
    $PHPUNIT_CMD
}

# Main execution
echo -e "${GREEN}Shown Connector Test Runner${NC}"
echo "================================="

if [ "$DOCKER_MODE" = true ]; then
    run_docker_tests
else
    run_standalone_tests
fi

echo -e "${GREEN}Tests completed!${NC}" 