#!/bin/bash

# Voice Command Testing Script
# Tests the voice classification API with various baby care commands

API_URL="${API_URL:-http://localhost:3020}"
ENDPOINT="/api/v1/voice/transcribe"

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

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Voice Command Testing Suite${NC}"
echo -e "${BLUE}========================================${NC}\n"

# Test commands
declare -a commands=(
    "Change wet diaper"
    "Baby ate 150ml formula"
    "Baby slept for 1 hour"
    "Alice slept for 30 min"
    "Alice ate 3 pcs of broccoli at 11:00 AM"
    "Dirty diaper change"
    "Fed baby 120ml"
    "Baby napped for 45 minutes"
    "Changed diaper, it was wet"
    "Gave baby vitamin D drops"
)

# Function to test a command
test_command() {
    local command="$1"
    local test_num="$2"

    echo -e "${YELLOW}Test #$test_num: \"$command\"${NC}"
    echo "---"

    # Make API request
    response=$(curl -s -X POST "${API_URL}${ENDPOINT}" \
        -H "Content-Type: application/json" \
        -d "{\"text\":\"$command\",\"language\":\"en\",\"childName\":\"Alice\"}")

    # Check if request was successful
    if [ $? -ne 0 ]; then
        echo -e "${RED}✗ API request failed${NC}\n"
        return 1
    fi

    # Parse response
    success=$(echo "$response" | jq -r '.success // false')

    if [ "$success" != "true" ]; then
        echo -e "${RED}✗ API returned error${NC}"
        echo "$response" | jq '.'
        echo ""
        return 1
    fi

    # Extract classification details
    type=$(echo "$response" | jq -r '.classification.type // "unknown"')
    confidence=$(echo "$response" | jq -r '.classification.confidence // 0')
    details=$(echo "$response" | jq -r '.classification.details // {}')
    timestamp=$(echo "$response" | jq -r '.classification.timestamp // "null"')

    # Color-code based on type
    case "$type" in
        feeding)
            type_color="${GREEN}"
            ;;
        sleep)
            type_color="${BLUE}"
            ;;
        diaper)
            type_color="${YELLOW}"
            ;;
        medicine)
            type_color="${RED}"
            ;;
        milestone)
            type_color="${GREEN}"
            ;;
        *)
            type_color="${RED}"
            ;;
    esac

    # Display results
    echo -e "Type:       ${type_color}${type}${NC}"
    echo -e "Confidence: ${confidence}"
    echo -e "Timestamp:  ${timestamp}"
    echo "Details:"
    echo "$details" | jq '.'

    # Validate confidence threshold
    confidence_float=$(echo "$confidence" | awk '{print ($1 >= 0.3) ? "pass" : "fail"}')

    if [ "$type" != "unknown" ] && [ "$confidence_float" == "pass" ]; then
        echo -e "${GREEN}✓ Command successfully classified${NC}\n"
        return 0
    else
        echo -e "${RED}✗ Low confidence or unknown type${NC}\n"
        return 1
    fi
}

# Run all tests
total_tests=${#commands[@]}
passed=0
failed=0

for i in "${!commands[@]}"; do
    test_num=$((i + 1))
    if test_command "${commands[$i]}" "$test_num"; then
        ((passed++))
    else
        ((failed++))
    fi
done

# Summary
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Test Summary${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "Total:  $total_tests"
echo -e "${GREEN}Passed: $passed${NC}"
echo -e "${RED}Failed: $failed${NC}"
echo ""

if [ $failed -eq 0 ]; then
    echo -e "${GREEN}All tests passed! 🎉${NC}"
    exit 0
else
    echo -e "${RED}Some tests failed. Check the output above.${NC}"
    exit 1
fi
