#!/bin/bash

# Test script for LLM email domain verification
# Run multiple test cases and save results

set -e

echo "======================================================"
echo "LLM Email Domain Verification Test Suite"
echo "======================================================"
echo ""

# Check if GEMINI_API_KEY is set
if [ -z "$GEMINI_API_KEY" ]; then
    echo "Error: GEMINI_API_KEY environment variable not set"
    echo ""
    echo "Usage: GEMINI_API_KEY=your_key ./run_tests.sh"
    exit 1
fi

# Create results directory
RESULTS_DIR="test_results_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$RESULTS_DIR"

echo "Results will be saved to: $RESULTS_DIR"
echo ""

# Test cases
declare -a test_domains=(
    "salesforce.com:Known company with clear domain"
    "stripe.com:Payment processing company"
    "databricks.com:Data analytics company"
    "pingcap.com:TiDB company (your own)"
    "fake-startup-xyz-2026.com:Non-existent domain"
    "test123456789.com:Non-existent test domain"
)

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

# Function to run a test
run_test() {
    local domain=$1
    local description=$2
    local result_file="$RESULTS_DIR/${domain}.txt"

    echo -e "${YELLOW}Testing: ${domain}${NC}"
    echo "Description: ${description}"
    echo ""

    # Run the test and capture output
    if go run main.go "$domain" > "$result_file" 2>&1; then
        echo -e "${GREEN}✓ Test completed${NC}"

        # Check if result contains "Unknown" for all fields
        if grep -q "Company Name.*: Unknown" "$result_file"; then
            echo "Result: Unknown (expected for non-existent domains)"
        else
            echo "Result: Company information found"
        fi
    else
        echo -e "${RED}✗ Test failed${NC}"
        echo "Error output saved to: $result_file"
    fi

    echo "Full output saved to: $result_file"
    echo "------------------------------------------------------"
    echo ""
}

# Run all tests
echo "Running test suite..."
echo ""

for test_case in "${test_domains[@]}"; do
    IFS=':' read -r domain description <<< "$test_case"
    run_test "$domain" "$description"

    # Add delay to avoid rate limiting
    sleep 2
done

# Summary
echo "======================================================"
echo "Test Summary"
echo "======================================================"
echo ""
echo "Total tests run: ${#test_domains[@]}"
echo "Results saved to: $RESULTS_DIR"
echo ""
echo "Review individual test results:"
ls -lh "$RESULTS_DIR"
echo ""

# Quick analysis
echo "======================================================"
echo "Quick Analysis"
echo "======================================================"
echo ""

successful_matches=0
unknown_results=0

for test_case in "${test_domains[@]}"; do
    IFS=':' read -r domain description <<< "$test_case"
    result_file="$RESULTS_DIR/${domain}.txt"

    if [ -f "$result_file" ]; then
        if grep -q "Company Name.*: Unknown" "$result_file"; then
            unknown_results=$((unknown_results + 1))
            echo "[$domain] → Unknown"
        elif grep -q "Company Name.*:" "$result_file" && ! grep -q "Company Name.*: Unknown" "$result_file"; then
            successful_matches=$((successful_matches + 1))
            company_name=$(grep "Company Name" "$result_file" | sed 's/.*: //')
            echo "[$domain] → $company_name"
        else
            echo "[$domain] → Error (check log)"
        fi
    fi
done

echo ""
echo "Successful matches: $successful_matches"
echo "Unknown results: $unknown_results"
echo "======================================================"
echo ""
echo "Done! Review the results in $RESULTS_DIR directory."
