#!/bin/bash

# Comprehensive Test Runner for AI Dictation Service
# Runs all test suites with proper error handling and reporting

echo "🧪 AI Dictation Service - Complete Test Runner"
echo "=================================================="
echo "This will run all test suites:"
echo "  - Original Dictation Tests"
echo "  - AI Conversation Tests"
echo "  - VLLM Integration Tests"
echo "=================================================="

# Function to run test and capture results
run_test() {
    local test_name=$1
    local test_file=$2
    local description=$3

    echo ""
    echo "📋 Running: $description"
    echo "   File: $test_file"
    echo "----------------------------------------"

    if [ -f "$test_file" ]; then
        if python "$test_file"; then
            echo "✅ $test_name: PASSED"
            return 0
        else
            echo "❌ $test_name: FAILED"
            return 1
        fi
    else
        echo "⚠️  $test_name: SKIPPED (file not found: $test_file)"
        return 2
    fi
}

# Test counter
total_tests=0
passed_tests=0
failed_tests=0
skipped_tests=0

# Run Original Dictation Tests
echo ""
echo "🎤 Testing Original Dictation Functionality..."
total_tests=$((total_tests + 1))
if run_test "DICTATION" "test_original_dictation.py" "Original voice-to-text dictation"; then
    passed_tests=$((passed_tests + 1))
elif [ $? -eq 1 ]; then
    failed_tests=$((failed_tests + 1))
else
    skipped_tests=$((skipped_tests + 1))
fi

# Run AI Conversation Tests
echo ""
echo "🤖 Testing AI Conversation Features..."
total_tests=$((total_tests + 1))
if run_test "AI_CONVERSATION" "test_suite.py" "AI conversation and VLLM integration"; then
    passed_tests=$((passed_tests + 1))
elif [ $? -eq 1 ]; then
    failed_tests=$((failed_tests + 1))
else
    skipped_tests=$((skipped_tests + 1))
fi

# Run VLLM Integration Tests
echo ""
echo "🔗 Testing VLLM Integration..."
total_tests=$((total_tests + 1))
if run_test "VLLM" "test_vllm_integration.py" "VLLM endpoint connectivity and performance"; then
    passed_tests=$((passed_tests + 1))
elif [ $? -eq 1 ]; then
    failed_tests=$((failed_tests + 1))
else
    skipped_tests=$((skipped_tests + 1))
fi

# System Status Checks
echo ""
echo "🔍 Running System Status Checks..."
echo "----------------------------------------"

# Check if VLLM is running
echo "🤖 Checking VLLM Service..."
if curl -s --connect-timeout 3 http://127.0.0.1:8000/health > /dev/null 2>&1; then
    echo "✅ VLLM service is running"
else
    echo "⚠️  VLLM service may not be running (this is expected if not started)"
fi

# Check audio system
echo "🎤 Checking Audio System..."
if command -v arecord > /dev/null 2>&1; then
    echo "✅ Audio recording available (arecord)"
else
    echo "⚠️  Audio recording not available"
fi

if command -v aplay > /dev/null 2>&1; then
    echo "✅ Audio playback available (aplay)"
else
    echo "⚠️  Audio playback not available"
fi

# Check notification system
echo "📢 Checking Notification System..."
if command -v notify-send > /dev/null 2>&1; then
    echo "✅ System notifications available (notify-send)"
else
    echo "⚠️  System notifications not available"
fi

# Check dictation service status
echo "🔧 Checking Dictation Service..."
if systemctl --user is-active --quiet dictation.service 2>/dev/null; then
    echo "✅ Dictation service is running"
elif systemctl --user is-enabled --quiet dictation.service 2>/dev/null; then
    echo "⚠️  Dictation service is enabled but not running"
else
    echo "⚠️  Dictation service not configured"
fi

# Test Results Summary
echo ""
echo "📊 TEST RESULTS SUMMARY"
echo "========================"
echo "Total Test Suites: $total_tests"
echo "Passed:            $passed_tests ✅"
echo "Failed:            $failed_tests ❌"
echo "Skipped:           $skipped_tests ⏭️"

# Overall status
if [ $failed_tests -eq 0 ]; then
    if [ $passed_tests -gt 0 ]; then
        echo ""
        echo "🎉 OVERALL STATUS: SUCCESS ✅"
        echo "All available tests passed!"
    else
        echo ""
        echo "⚠️  OVERALL STATUS: NO TESTS RUN"
        echo "Test files may not be available or dependencies missing"
    fi
else
    echo ""
    echo "❌ OVERALL STATUS: TEST FAILURES DETECTED"
    echo "Some tests failed. Please review the output above."
fi

# Recommendations
echo ""
echo "💡 RECOMMENDATIONS"
echo "=================="
echo "1. Ensure all dependencies are installed: uv sync"
echo "2. Start VLLM service for full functionality"
echo "3. Enable dictation service: systemctl --user enable dictation.service"
echo "4. Test with actual microphone input for real-world validation"

# Quick test commands
echo ""
echo "⚡ QUICK TEST COMMANDS"
echo "====================="
echo "# Test individual components:"
echo "python test_original_dictation.py"
echo "python test_suite.py"
echo "python test_vllm_integration.py"
echo ""
echo "# Test service status:"
echo "systemctl --user status dictation.service"
echo "journalctl --user -u dictation.service -f"
echo ""
echo "# Test VLLM endpoint:"
echo "curl -H 'Authorization: Bearer vllm-api-key' http://127.0.0.1:8000/v1/models"

echo ""
echo "🏁 Test runner complete!"
echo "======================="