#!/bin/bash

# Comprehensive test runner for jbuild interactive capabilities
# This script runs all tests and provides detailed output

set -e

echo "🚀 Starting comprehensive test suite for jbuild interactive capabilities"
echo "====================================================================="

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

# Test counters
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# Function to print test results
print_test_result() {
    local test_name="$1"
    local result="$2"
    local message="$3"
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    if [ "$result" = "PASS" ]; then
        echo -e "${GREEN}✅ $test_name: $message${NC}"
        PASSED_TESTS=$((PASSED_TESTS + 1))
    else
        echo -e "${RED}❌ $test_name: $message${NC}"
        FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
}

# Function to run cargo test with timeout
run_test() {
    local test_name="$1"
    local test_args="$2"
    local timeout_seconds="${3:-30}"
    
    echo -e "${BLUE}🧪 Running: $test_name${NC}"
    echo "Command: cargo test $test_args -- --test-threads=1"
    
    if timeout $timeout_seconds cargo test $test_args -- --test-threads=1 2>/dev/null; then
        print_test_result "$test_name" "PASS" "Completed successfully"
    else
        print_test_result "$test_name" "FAIL" "Failed or timed out"
    fi
}

# Function to check if binary exists
check_binary() {
    if [ -f "./target/debug/jbuild" ]; then
        return 0
    else
        echo -e "${YELLOW}⚠️  Building jbuild binary first...${NC}"
        cargo build --bin jbuild
    fi
}

# Main test execution
main() {
    echo -e "${BLUE}🔍 Checking prerequisites...${NC}"
    check_binary
    
    echo ""
    echo -e "${BLUE}📋 Running unit tests...${NC}"
    echo "====================================================================="
    
    # Unit tests
    run_test "Build Metrics" "interactive_unit_tests" "20"
    run_test "Profile Reports" "interactive_unit_tests" "20"
    run_test "Monitoring Functions" "interactive_unit_tests" "20"
    run_test "Error Handling" "interactive_unit_tests" "20"
    run_test "Performance Calculations" "interactive_unit_tests" "20"
    
    echo ""
    echo -e "${BLUE}🔗 Running integration tests...${NC}"
    echo "====================================================================="
    
    # Integration tests
    run_test "Interactive Maven Project" "interactive_integration_tests" "60"
    run_test "Interactive Gradle Project" "interactive_integration_tests" "60"
    run_test "Monitor Integration" "interactive_integration_tests" "40"
    run_test "Profile Integration" "interactive_integration_tests" "40"
    run_test "Profile Output Formats" "interactive_integration_tests" "40"
    run_test "Error Handling Integration" "interactive_integration_tests " "30"
    
    echo ""
    echo -e "${BLUE}⚡ Running performance tests...${NC}"
    echo "====================================================================="
    
    # Performance tests
    run_test "Metrics Performance" "interactive_performance_tests" "30"
    run_test "Dashboard Rendering" "interactive_performance_tests" "30"
    run_test "Monitoring Throughput" "interactive_performance_tests" "30"
    run_test "Concurrent Monitoring" "interactive_performance_tests" "45"
    run_test "Large Project Metrics" "interactive_performance_tests" "60"
    
    echo ""
    echo -e "${BLUE}🌐 Running interactive command tests...${NC}"
    echo "====================================================================="
    
    # Interactive command tests
    echo -e "${BLUE}🧪 Testing interactive command help...${NC}"
    if timeout 30s cargo run --bin jbuild -- interactive --help >/dev/null 2>&1; then
        print_test_result "Interactive Help" "PASS" "Command help works"
    else
        print_test_result "Interactive Help" "FAIL" "Command help failed"
    fi
    
    echo -e "${BLUE}🧪 Testing monitor command help...${NC}"
    if timeout 30s cargo run --bin jbuild -- monitor --help >/dev/null 2>&1; then
        print_test_result "Monitor Help" "PASS" "Command help works"
    else
        print_test_result "Monitor Help" "FAIL" "Command help failed"
    fi
    
    echo -e "${BLUE}🧪 Testing profile command help...${NC}"
    if timeout 30s cargo run --bin jbuild -- profile --help >/dev/null 2>&1; then
        print_test_result "Profile Help" "PASS" "Command help works"
    else
        print_test_result "Profile Help" "FAIL" "Command help failed"
    fi
    
    echo ""
    echo -e "${BLUE}🎯 Running specific interactive mode tests...${NC}"
    echo "====================================================================="
    
    # Test interactive modes
    if timeout 15s cargo run --bin jbuild -- monitor --duration 2 >/dev/null 2>&1; then
        print_test_result "Monitor Mode" "PASS" "Real-time monitoring works"
    else
        print_test_result "Monitor Mode" "FAIL" "Real-time monitoring failed"
    fi
    
    if timeout 15s cargo run --bin jbuild -- interactive --dashboard >/dev/null 2>&1; then
        print_test_result "Dashboard Mode" "PASS" "Dashboard mode works"
    else
        print_test_result "Dashboard Mode" "FAIL" "Dashboard mode failed"
    fi
    
    echo ""
    echo -e "${BLUE}📊 Running comprehensive tests...${NC}"
    echo "====================================================================="
    
    # Run all interactive tests together
    run_test "Complete Interactive Suite" "interactive_tests" "90"
    
    # Summary
    echo ""
    echo "====================================================================="
    echo -e "${BLUE}🎉 Test Summary${NC}"
    echo "====================================================================="
    echo "Total Tests: $TOTAL_TESTS"
    echo -e "${GREEN}Passed: $PASSED_TESTS${NC}"
    echo -e "${RED}Failed: $FAILED_TESTS${NC}"
    
    if [ $FAILED_TESTS -eq 0 ]; then
        echo -e "${GREEN}🎉 All tests passed! Interactive capabilities are working correctly.${NC}"
        exit 0
    else
        echo -e "${RED}❌ Some tests failed. Please review the output above.${NC}"
        exit 1
    fi
}

# Run main function
main "$@"