#!/bin/bash
#
# Crumbforest Test Script
# Führt verschiedene Tests aus
#

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

print_success() {
    echo -e "${GREEN}✓${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

echo "============================================"
echo "🦉 Crumbforest Tests"
echo "============================================"
echo ""

# Check if system is running
if ! curl -s http://localhost:8000/health > /dev/null 2>&1; then
    print_error "System läuft nicht!"
    echo "  Starte es mit: ./start.sh"
    exit 1
fi

print_success "System läuft"
echo ""

# Test Selection
if [ -z "$1" ]; then
    echo "Welche Tests sollen ausgeführt werden?"
    echo ""
    echo "  1) Quick Test       - Basis-Tests (Health, API)"
    echo "  2) Integration Test - Kompletter RAG Flow"
    echo "  3) Alle Tests       - Quick + Integration"
    echo ""
    read -p "Auswahl (1-3): " choice
else
    choice=$1
fi

case $choice in
    1|quick)
        TEST_TYPE="quick"
        ;;
    2|integration)
        TEST_TYPE="integration"
        ;;
    3|all)
        TEST_TYPE="all"
        ;;
    *)
        print_error "Ungültige Auswahl"
        exit 1
        ;;
esac

# Quick Tests
if [ "$TEST_TYPE" = "quick" ] || [ "$TEST_TYPE" = "all" ]; then
    echo "=== Quick Tests ==="
    echo ""

    # Health Check
    echo -n "Health Check... "
    if curl -s http://localhost:8000/health | grep -q "ok"; then
        print_success "OK"
    else
        print_error "FAILED"
    fi

    # Routes
    echo -n "Routes Check... "
    routes=$(curl -s http://localhost:8000/__routes | jq length 2>/dev/null || echo "0")
    if [ "$routes" -gt 0 ]; then
        print_success "OK ($routes routes)"
    else
        print_error "FAILED"
    fi

    # Database
    echo -n "Database Check... "
    if docker compose -f compose/docker-compose.yml exec -T db sh -c 'mariadb -u$MARIADB_USER -p$MARIADB_PASSWORD $MARIADB_DATABASE -e "SELECT COUNT(*) FROM users" 2>/dev/null' | grep -q "2"; then
        print_success "OK"
    else
        print_warning "Users table check failed"
    fi

    # Qdrant
    echo -n "Qdrant Check... "
    if curl -s http://localhost:6333/collections > /dev/null 2>&1; then
        print_success "OK"
    else
        print_error "FAILED"
    fi

    # Provider Status
    echo -n "Provider Check... "
    if curl -s http://localhost:8000/admin/rag/providers 2>/dev/null | grep -q "available"; then
        print_success "OK"
    else
        print_warning "Keine Provider konfiguriert"
    fi

    echo ""
fi

# Integration Tests
if [ "$TEST_TYPE" = "integration" ] || [ "$TEST_TYPE" = "all" ]; then
    echo "=== Integration Tests ==="
    echo ""

    # Check Python
    if ! command -v python3 &> /dev/null; then
        print_error "Python 3 nicht gefunden"
        echo "  Integration Tests benötigen Python 3"
        exit 1
    fi

    # Setup Virtual Environment
    if [ ! -d ".venv" ]; then
        echo -n "Erstelle Virtual Environment (.venv)... "
        python3 -m venv .venv
        print_success "OK"
    fi

    # Install dependencies
    if ! .venv/bin/python3 -c "import requests, pymysql" 2>/dev/null; then
        echo "Installiere Abhängigkeiten in .venv..."
        .venv/bin/pip install -q requests pymysql
        print_success "Dependencies installiert"
    fi

    # Check API Keys
    if ! grep -v "^#" compose/.env | grep -q "API_KEY=sk"; then
        print_warning "Keine API Keys in compose/.env gefunden"
        echo ""
        echo "Integration Tests benötigen mindestens einen API Key:"
        echo "  - OPENAI_API_KEY"
        echo "  - ANTHROPIC_API_KEY"
        echo "  - OPENROUTER_API_KEY"
        echo ""
        read -p "Trotzdem fortfahren? (y/n): " continue
        if [ "$continue" != "y" ]; then
            exit 1
        fi
    fi

    # Run Python integration test
    echo "Führe Python Integration Test aus..."
    echo ""

    # Set API key from .env if available
    # Set API key from .env if available
    # Check OpenRouter first (specific prefix)
    if grep -q "OPENROUTER_API_KEY=sk-or-v1" compose/.env; then
        export OPENROUTER_API_KEY=$(grep "OPENROUTER_API_KEY=" compose/.env | cut -d'=' -f2)
        export TEST_PROVIDER="openrouter"
    elif grep -q "ANTHROPIC_API_KEY=sk" compose/.env; then
        export ANTHROPIC_API_KEY=$(grep "ANTHROPIC_API_KEY=" compose/.env | cut -d'=' -f2)
        export TEST_PROVIDER="claude"
    elif grep -q "OPENAI_API_KEY=sk" compose/.env; then
        # Check if it's actually an OpenRouter key
        if grep -q "OPENAI_API_KEY=sk-or-v1" compose/.env; then
            print_error "Konfiguration ungültig!"
            echo "  Du hast einen OpenRouter Key in OPENAI_API_KEY gespeichert."
            echo "  Bitte benenne die Variable in compose/.env in OPENROUTER_API_KEY um."
            exit 1
        fi
        export OPENAI_API_KEY=$(grep "OPENAI_API_KEY=" compose/.env | cut -d'=' -f2)
        export TEST_PROVIDER="openai"
    fi

    if .venv/bin/python3 tests/test_integration.py; then
        echo ""
        print_success "Integration Tests erfolgreich"
    else
        echo ""
        print_error "Integration Tests fehlgeschlagen"
        exit 1
    fi

    echo ""
fi

# Summary
echo "============================================"
print_success "Alle Tests erfolgreich!"
echo "============================================"
echo ""
echo "Weitere Tests:"
echo "  curl http://localhost:8000/__routes        - Alle Routes"
echo "  curl http://localhost:8000/__whoami        - Session Info"
echo "  curl http://localhost:8000/admin/rag/providers - Provider Status"
echo ""
