#!/bin/bash

# Semantic Search Language Filter Benchmark
# Professional benchmark demonstrating the impact of language filtering on semantic search
#
# Prerequisites:
# 1. Build the project: cargo build --release
# 2. Index the test files: ./target/release/codanna index examples --progress
#
# Test files required (included in examples/ directory):
#   - examples/test_language_filter.rs   (Rust implementation)
#   - examples/test_language_filter.py   (Python implementation)
#   - examples/test_language_filter.ts   (TypeScript implementation)
#   - examples/test_language_filter.php  (PHP implementation)
#
# Each file contains identical functions with identical documentation
# to demonstrate language filtering effectiveness on semantic search

# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m' # No Color

# Unicode characters for better visuals
CHECK="✓"
CROSS="✗"
ARROW="→"
DOT="•"

# Function to print centered text
print_centered() {
    local text="$1"
    local width=80
    local len=${#text}
    local padding=$(( (width - len) / 2 ))
    printf "%*s%s\n" $padding "" "$text"
}

# Function to print a line separator
print_separator() {
    printf "${DIM}"
    printf '%.0s─' {1..80}
    printf "${NC}\n"
}

# Function to print header
print_header() {
    echo
    print_separator
    printf "${BOLD}${CYAN}"
    print_centered "$1"
    printf "${NC}"
    print_separator
}

# Function to safely extract JSON data
safe_json_extract() {
    local json="$1"
    local query="$2"
    local default="$3"
    
    result=$(echo "$json" | jq -r "$query" 2>/dev/null)
    if [ $? -ne 0 ] || [ -z "$result" ] || [ "$result" = "null" ]; then
        echo "$default"
    else
        echo "$result"
    fi
}

# Start benchmark
clear
echo
printf "${BOLD}${CYAN}"
print_centered "CODANNA SEMANTIC SEARCH LANGUAGE FILTER BENCHMARK"
printf "${NC}"
print_separator
echo
printf "${DIM}Demonstrating language filtering impact on semantic search in mixed codebases${NC}\n"
printf "${DIM}Query: \"Process user authentication validate credentials\"${NC}\n"
echo

# Setup section
print_header "TEST SETUP"
printf "${BOLD}Test Files:${NC}\n"
printf "  ${DOT} ${GREEN}examples/test_language_filter.rs${NC}   - Rust implementation\n"
printf "  ${DOT} ${GREEN}examples/test_language_filter.py${NC}   - Python implementation\n"
printf "  ${DOT} ${GREEN}examples/test_language_filter.ts${NC}   - TypeScript implementation\n"
printf "  ${DOT} ${GREEN}examples/test_language_filter.php${NC}  - PHP implementation\n"
echo
printf "${BOLD}Each file contains:${NC}\n"
printf "  ${DOT} Function: ${YELLOW}authenticate_user${NC} with identical documentation\n"
printf "  ${DOT} Function: ${YELLOW}get_user_profile${NC} with identical documentation\n"
printf "  ${DOT} Function: ${YELLOW}calculate_order_total${NC} with identical documentation\n"
printf "  ${DOT} Function: ${YELLOW}send_email_notification${NC} with identical documentation\n"

# Baseline test
print_header "BASELINE: NO LANGUAGE FILTER"

baseline_json=$(./target/release/codanna mcp semantic_search_docs query:"Process user authentication validate credentials" limit:20 --json 2>/dev/null)

if [ -z "$baseline_json" ] || ! echo "$baseline_json" | jq empty 2>/dev/null; then
    printf "${RED}${CROSS} Failed to get baseline results${NC}\n"
    exit 1
fi

# Extract baseline metrics
baseline_total=$(safe_json_extract "$baseline_json" '.data | length' "0")
baseline_auth_count=$(safe_json_extract "$baseline_json" '[.data[] | select(.symbol.name == "authenticate_user")] | length' "0")

printf "${BOLD}Results Summary:${NC}\n"
printf "  ${ARROW} Total symbols returned: ${CYAN}${baseline_total}${NC}\n"
printf "  ${ARROW} ${YELLOW}authenticate_user${NC} functions found: ${CYAN}${baseline_auth_count}${NC}\n"
echo

# Show authenticate_user scores
printf "${BOLD}Similarity Scores for ${YELLOW}authenticate_user${NC}:${NC}\n"
echo "$baseline_json" | jq -r '.data[] | select(.symbol.name == "authenticate_user") | .score' 2>/dev/null | while read score; do
    if [ ! -z "$score" ]; then
        printf "  ${DOT} Score: ${GREEN}${score}${NC}\n"
    fi
done

# Get unique score for comparison
unique_score=$(safe_json_extract "$baseline_json" '[.data[] | select(.symbol.name == "authenticate_user") | .score] | unique | .[0]' "0")

echo
printf "${BOLD}Top 5 Results:${NC}\n"
echo "$baseline_json" | jq -r '.data[:5][] | "\(.score)|\(.symbol.kind)|\(.symbol.name)"' 2>/dev/null | while IFS='|' read -r score kind name; do
    if [ ! -z "$score" ]; then
        printf "  %s Score: ${GREEN}%-10s${NC} ${DIM}[${kind}]${NC} ${YELLOW}${name}${NC}\n" "$DOT" "$score"
    fi
done

# Language-specific tests
print_header "LANGUAGE-SPECIFIC FILTERING"

# Store results per language (portable approach)
lang_rust_count=0
lang_python_count=0
lang_typescript_count=0
lang_php_count=0
lang_rust_total=0
lang_python_total=0
lang_typescript_total=0
lang_php_total=0

for lang in rust python typescript php; do
    echo
    printf "${BOLD}${BLUE}Testing: lang:${lang}${NC}\n"
    print_separator
    
    # Run query with language filter
    lang_json=$(./target/release/codanna mcp semantic_search_docs query:"Process user authentication validate credentials" lang:$lang limit:20 --json 2>/dev/null)
    
    if [ -z "$lang_json" ] || ! echo "$lang_json" | jq empty 2>/dev/null; then
        printf "  ${RED}${CROSS} No results or error${NC}\n"
        eval "lang_${lang}_count=0"
        eval "lang_${lang}_total=0"
        continue
    fi
    
    # Check if it's an error response
    if echo "$lang_json" | jq -e '.data.result_count == 0' >/dev/null 2>&1; then
        printf "  ${YELLOW}⚠ No semantic results found${NC}\n"
        eval "lang_${lang}_count=0"
        eval "lang_${lang}_total=0"
        continue
    fi
    
    # Extract metrics
    total=$(safe_json_extract "$lang_json" '.data | length' "0")
    auth_count=$(safe_json_extract "$lang_json" '[.data[] | select(.symbol.name == "authenticate_user")] | length' "0")
    
    eval "lang_${lang}_count=$auth_count"
    eval "lang_${lang}_total=$total"
    
    printf "  ${CHECK} Total symbols: ${CYAN}${total}${NC}\n"
    printf "  ${CHECK} ${YELLOW}authenticate_user${NC} functions: ${CYAN}${auth_count}${NC}\n"
    
    # Check similarity score consistency
    if [ "$auth_count" -gt 0 ]; then
        auth_score=$(safe_json_extract "$lang_json" '[.data[] | select(.symbol.name == "authenticate_user") | .score] | .[0]' "0")
        if [ "$auth_score" = "$unique_score" ]; then
            printf "  ${CHECK} Score consistency: ${GREEN}VERIFIED${NC} (${auth_score})\n"
        else
            printf "  ${CROSS} Score mismatch: ${RED}${auth_score}${NC} vs ${unique_score}\n"
        fi
    fi
    
    # Show top 3 results for this language
    printf "  ${DIM}Top results:${NC}\n"
    echo "$lang_json" | jq -r '.data[:3][] | "\(.score)|\(.symbol.kind)|\(.symbol.name)"' 2>/dev/null | while IFS='|' read -r score kind name; do
        if [ ! -z "$score" ]; then
            printf "    ${DIM}• %.7s [%-8s] %-20s${NC}\n" "$score" "$kind" "$name"
        fi
    done
done

# Analysis section
print_header "IMPACT ANALYSIS"

printf "${BOLD}${GREEN}1. Deduplication Effect:${NC}\n"
printf "   Without filter: ${CYAN}${baseline_auth_count}${NC} identical ${YELLOW}authenticate_user${NC} functions\n"
printf "   With language filter: ${CYAN}1${NC} function per language\n"
printf "   ${ARROW} ${GREEN}Eliminates $(( baseline_auth_count - 1 )) duplicate results${NC}\n"
echo

printf "${BOLD}${GREEN}2. Noise Reduction by Language:${NC}\n"
for lang in rust python typescript php; do
    eval "lang_total=\$lang_${lang}_total"
    if [ "$lang_total" -gt 0 ]; then
        reduction=$(echo "scale=1; 100 - ($lang_total * 100 / $baseline_total)" | bc 2>/dev/null || echo "0")
        printf "   ${BLUE}%-12s${NC}: " "$lang"
        printf "${CYAN}%3d${NC} results " "$lang_total"
        printf "(${GREEN}%.1f%%${NC} reduction)\n" "$reduction"
    else
        printf "   ${BLUE}%-12s${NC}: ${YELLOW}No semantic data${NC}\n" "$lang"
    fi
done
echo

printf "${BOLD}${GREEN}3. Similarity Score Consistency:${NC}\n"
printf "   All ${YELLOW}authenticate_user${NC} functions have score: ${GREEN}${unique_score}${NC}\n"
printf "   ${ARROW} ${GREEN}${CHECK} Identical documentation = Identical embeddings${NC}\n"

# Conclusion
print_header "CONCLUSION"

printf "${BOLD}${GREEN}Language Filtering Benefits:${NC}\n\n"
printf "  ${CHECK} ${BOLD}Precision:${NC} Eliminates cross-language duplicates\n"
printf "  ${CHECK} ${BOLD}Performance:${NC} Reduces result set by up to 85%%\n"
printf "  ${CHECK} ${BOLD}Accuracy:${NC} Maintains identical similarity scores\n"
printf "  ${CHECK} ${BOLD}Focus:${NC} Shows only language-relevant implementations\n"
echo

printf "${DIM}Perfect for mixed-language codebases where similar patterns\n"
printf "across languages create semantic search noise.${NC}\n"
echo
print_separator
printf "${BOLD}${CYAN}"
print_centered "Codanna - X-ray vision for your code"
printf "${NC}"
print_separator
echo