#!/bin/bash

# Patent Module Test Runner
# This script provides various options for running tests for the Patent module

set -e

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

# Default values
TEST_TYPE="all"
COVERAGE=false
VERBOSE=false
PARALLEL=false
FILTER=""

# Function to display help
show_help() {
    echo -e "${BLUE}Patent Module Test Runner${NC}"
    echo ""
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -t, --type TYPE        Test type: unit, feature, integration, all (default: all)"
    echo "  -c, --coverage         Generate code coverage report"
    echo "  -v, --verbose          Verbose output"
    echo "  -p, --parallel         Run tests in parallel"
    echo "  -f, --filter FILTER    Filter tests by name or group"
    echo "  -h, --help             Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0                                    # Run all tests"
    echo "  $0 -t unit                           # Run only unit tests"
    echo "  $0 -t feature -c                     # Run feature tests with coverage"
    echo "  $0 -f PatentTransactionTest          # Run specific test class"
    echo "  $0 -t unit -v -p                     # Run unit tests with verbose output in parallel"
    echo ""
}

# Function to run tests
run_tests() {
    local test_command="cd ../../ && php vendor/bin/phpunit"
    
    # Add test type filter
    case $TEST_TYPE in
        "unit")
            test_command="$test_command --testsuite 'Patent Module Unit Tests'"
            ;;
        "feature")
            test_command="$test_command --testsuite 'Patent Module Feature Tests'"
            ;;
        "integration")
            test_command="$test_command --testsuite 'Patent Module Integration Tests'"
            ;;
        "all")
            test_command="$test_command --testsuite 'Patent Module All Tests'"
            ;;
    esac
    
    # Add coverage if requested
    if [ "$COVERAGE" = true ]; then
        test_command="$test_command --coverage-html build/coverage --coverage-clover build/coverage.xml"
    fi
    
    # Add verbose if requested
    if [ "$VERBOSE" = true ]; then
        test_command="$test_command --verbose"
    fi
    
    # Add parallel if requested
    if [ "$PARALLEL" = true ]; then
        test_command="$test_command --processes=4"
    fi
    
    # Add filter if provided
    if [ -n "$FILTER" ]; then
        test_command="$test_command --filter '$FILTER'"
    fi
    
    # Add configuration file
    test_command="$test_command --configuration Modules/Patent/phpunit.xml"
    
    echo -e "${BLUE}Running command:${NC} $test_command"
    echo ""
    
    # Execute the command
    eval $test_command
}

# Function to setup test environment
setup_test_environment() {
    echo -e "${YELLOW}Setting up test environment...${NC}"
    
    # Create build directory if it doesn't exist
    mkdir -p build
    
    # Clear any existing coverage reports
    rm -rf build/coverage build/coverage.xml build/junit.xml
    
    # Run migrations for testing
    echo -e "${YELLOW}Running migrations...${NC}"
    # Run essential migrations step by step
    php ../../artisan migrate:fresh --env=testing --force --path=database/migrations/2014_05_12_074614_create_permission_tables.php
    php ../../artisan migrate --env=testing --force --path=database/migrations/2014_10_11_141806_create_departments_table.php
    php ../../artisan migrate --env=testing --force --path=database/migrations/2014_10_12_000000_create_users_table.php
    # Run Patent module test migrations
    php ../../artisan migrate --env=testing --force --path=Modules/Patent/Database/TestMigrations
    
    echo -e "${GREEN}Test environment ready!${NC}"
    echo ""
}

# Function to cleanup after tests
cleanup_after_tests() {
    echo -e "${YELLOW}Cleaning up...${NC}"
    
    # Remove test database (not needed for in-memory SQLite)
    # rm -f database/testing.sqlite
    
    echo -e "${GREEN}Cleanup complete!${NC}"
}

# Function to show test results
show_test_results() {
    if [ -f "build/coverage/index.html" ]; then
        echo -e "${GREEN}Coverage report generated: build/coverage/index.html${NC}"
    fi
    
    if [ -f "build/coverage.xml" ]; then
        echo -e "${GREEN}Coverage XML generated: build/coverage.xml${NC}"
    fi
    
    if [ -f "build/junit.xml" ]; then
        echo -e "${GREEN}JUnit XML generated: build/junit.xml${NC}"
    fi
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -t|--type)
            TEST_TYPE="$2"
            shift 2
            ;;
        -c|--coverage)
            COVERAGE=true
            shift
            ;;
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        -p|--parallel)
            PARALLEL=true
            shift
            ;;
        -f|--filter)
            FILTER="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo -e "${RED}Unknown option: $1${NC}"
            show_help
            exit 1
            ;;
    esac
done

# Validate test type
case $TEST_TYPE in
    "unit"|"feature"|"integration"|"all")
        ;;
    *)
        echo -e "${RED}Invalid test type: $TEST_TYPE${NC}"
        echo "Valid types: unit, feature, integration, all"
        exit 1
        ;;
esac

# Main execution
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}    Patent Module Test Runner${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Setup test environment
setup_test_environment

# Run tests
echo -e "${BLUE}Running $TEST_TYPE tests...${NC}"
run_tests

# Show results
show_test_results

# Cleanup
cleanup_after_tests

echo ""
echo -e "${GREEN}Test execution completed!${NC}"
