#!/bin/bash

# HVAC Community Events Test Runner
# This script runs the plugin's test suite

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

# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PLUGIN_DIR="$(dirname "$SCRIPT_DIR")"

echo -e "${GREEN}HVAC Community Events Test Suite${NC}"
echo "=================================="

# Check if PHPUnit is available
if ! command -v phpunit &> /dev/null; then
    echo -e "${RED}Error: PHPUnit is not installed or not in PATH${NC}"
    echo "Please install PHPUnit or use the vendor/bin/phpunit"
    exit 1
fi

# Change to plugin directory
cd "$PLUGIN_DIR"

# Run different test suites based on arguments
if [ "$1" = "unit" ]; then
    echo -e "${YELLOW}Running Unit Tests...${NC}"
    phpunit --testsuite=unit
elif [ "$1" = "integration" ]; then
    echo -e "${YELLOW}Running Integration Tests...${NC}"
    phpunit --testsuite=integration
elif [ "$1" = "coverage" ]; then
    echo -e "${YELLOW}Running Tests with Code Coverage...${NC}"
    phpunit --coverage-html coverage-report --coverage-text
    echo -e "${GREEN}Coverage report generated in coverage-report/${NC}"
elif [ "$1" = "specific" ] && [ -n "$2" ]; then
    echo -e "${YELLOW}Running Specific Test: $2${NC}"
    phpunit "$2"
else
    echo -e "${YELLOW}Running All Tests...${NC}"
    phpunit
fi

# Check exit status
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Tests passed successfully!${NC}"
else
    echo -e "${RED}✗ Tests failed!${NC}"
    exit 1
fi