#!/usr/bin/env python3
"""
Test script for Crumbforest Chat API
Tests both Krümeleule and FunkFox characters
"""
import requests
import json
import sys


API_URL = "http://localhost:8000/api/chat"


def test_character(character_id: str, question: str):
    """Test a character with a question."""
    print(f"\n{'='*60}")
    print(f"🧪 Testing: {character_id.upper()}")
    print(f"📝 Question: {question}")
    print(f"{'='*60}\n")

    payload = {
        "character_id": character_id,
        "question": question,
        "lang": "de"
    }

    try:
        response = requests.post(API_URL, json=payload, timeout=30)
        response.raise_for_status()

        result = response.json()

        print(f"✅ Response received!")
        print(f"🤖 Provider: {result['provider']}")
        print(f"🧠 Model: {result['model']}")
        print(f"📚 Context found: {result['context_found']}")
        print(f"📄 Sources: {len(result['sources'])}")
        print(f"\n💬 Answer:\n")
        print(result['answer'])

        if result['sources']:
            print(f"\n📚 Sources used:")
            for idx, source in enumerate(result['sources'], 1):
                print(f"  {idx}. {source['file_path']} (score: {source['score']:.3f})")

        return True

    except requests.exceptions.RequestException as e:
        print(f"❌ Error: {e}")
        if hasattr(e, 'response') and e.response is not None:
            print(f"Response: {e.response.text}")
        return False


def main():
    """Run all tests."""
    print("🌲🦊🦉 Crumbforest Chat API Test Suite")
    print("=" * 60)

    # Test 1: Krümeleule
    success_eule = test_character(
        "eule",
        "Was ist ADHS als Kraftquelle?"
    )

    # Test 2: FunkFox
    success_fox = test_character(
        "funkfox",
        "Was ist ein Terminal und wie nutzt man es?"
    )

    # Summary
    print(f"\n{'='*60}")
    print("📊 TEST SUMMARY")
    print(f"{'='*60}")
    print(f"🦉 Krümeleule: {'✅ PASS' if success_eule else '❌ FAIL'}")
    print(f"🦊 FunkFox: {'✅ PASS' if success_fox else '❌ FAIL'}")
    print(f"{'='*60}\n")

    if success_eule and success_fox:
        print("🎉 All tests passed! BOOOYAAAA! 🚀")
        return 0
    else:
        print("⚠️  Some tests failed.")
        return 1


if __name__ == "__main__":
    sys.exit(main())
