#!/bin/bash
# Test script to run both Rust and Go implementations with their respective configs
# This demonstrates feature parity between the implementations

set -e

echo "🧪 Testing both Rust and Go implementations with identical configurations"
echo "=================================================="

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

# Check if test environment is running
check_test_env() {
    echo -e "${BLUE}📡 Checking test environment...${NC}"
    
    if ! curl -s http://localhost:5984 >/dev/null; then
        echo -e "${YELLOW}⚠️  Test environment not running. Starting it...${NC}"
        cd test
        ./start-test-env.sh
        cd ..
        echo -e "${GREEN}✅ Test environment started${NC}"
    else
        echo -e "${GREEN}✅ Test environment is running${NC}"
    fi
}

# Build both implementations
build_implementations() {
    echo -e "${BLUE}🔨 Building implementations...${NC}"
    
    # Build Go implementation
    echo -e "${BLUE}  Building Go implementation...${NC}"
    cd go
    go build -o mail2couch .
    cd ..
    echo -e "${GREEN}  ✅ Go implementation built${NC}"
    
    # Build Rust implementation
    echo -e "${BLUE}  Building Rust implementation...${NC}"
    cd rust
    cargo build --release
    cd ..
    echo -e "${GREEN}  ✅ Rust implementation built${NC}"
}

# Run Go implementation
run_go() {
    echo -e "${BLUE}🦬 Running Go implementation...${NC}"
    cd go
    echo -e "${BLUE}  Using config: config-test-go.json${NC}"
    ./mail2couch -c config-test-go.json
    cd ..
    echo -e "${GREEN}✅ Go implementation completed${NC}"
}

# Run Rust implementation
run_rust() {
    echo -e "${BLUE}🦀 Running Rust implementation...${NC}"
    cd rust
    echo -e "${BLUE}  Using config: config-test-rust.json${NC}"
    ./target/release/mail2couch -c config-test-rust.json
    cd ..
    echo -e "${GREEN}✅ Rust implementation completed${NC}"
}

# Check results
check_results() {
    echo -e "${BLUE}🔍 Checking results...${NC}"
    
    echo -e "${BLUE}  Listing all databases:${NC}"
    curl -s http://localhost:5984/_all_dbs | python3 -m json.tool
    
    echo -e "\n${BLUE}  Go implementation databases:${NC}"
    for db in go_wildcard_all_folders_test go_work_pattern_test go_specific_folders_only; do
        db_name="m2c_${db}"
        if curl -s "http://localhost:5984/${db_name}" >/dev/null 2>&1; then
            doc_count=$(curl -s "http://localhost:5984/${db_name}" | python3 -c "import sys, json; print(json.load(sys.stdin).get('doc_count', 0))")
            echo -e "${GREEN}    ✅ ${db_name}: ${doc_count} documents${NC}"
        else
            echo -e "${RED}    ❌ ${db_name}: not found${NC}"
        fi
    done
    
    echo -e "\n${BLUE}  Rust implementation databases:${NC}"
    for db in rust_wildcard_all_folders_test rust_work_pattern_test rust_specific_folders_only; do
        db_name="m2c_${db}"
        if curl -s "http://localhost:5984/${db_name}" >/dev/null 2>&1; then
            doc_count=$(curl -s "http://localhost:5984/${db_name}" | python3 -c "import sys, json; print(json.load(sys.stdin).get('doc_count', 0))")
            echo -e "${GREEN}    ✅ ${db_name}: ${doc_count} documents${NC}"
        else
            echo -e "${RED}    ❌ ${db_name}: not found${NC}"
        fi
    done
}

# Main execution
main() {
    echo -e "${YELLOW}🚀 Starting feature parity test...${NC}"
    
    check_test_env
    build_implementations
    
    echo -e "\n${YELLOW}📊 Running implementations with identical configurations...${NC}"
    run_go
    echo ""
    run_rust
    
    echo -e "\n${YELLOW}📈 Checking results...${NC}"
    check_results
    
    echo -e "\n${GREEN}🎉 Feature parity test completed!${NC}"
    echo -e "${BLUE}💡 Both implementations should have created similar databases with identical message counts.${NC}"
    echo -e "${BLUE}🔗 View results at: http://localhost:5984/_utils${NC}"
}

# Execute main function
main "$@"