
import sys
import os
import asyncio
import json
from pathlib import Path

# Add app to path
# Add app to path (local execution)
sys.path.insert(0, os.path.join(os.getcwd(), 'app'))

from services.provider_factory import ProviderFactory
from utils.rag_chat import RAGChatService
from deps import get_qdrant_client
from config import get_settings

async def test_roles():
    print("🌲 Testing ALL Roles with Gemini 2.0 & RAG...")
    
    # Load Config
    with open("crumbforest_config.json", "r") as f:
        config = json.load(f)
    
    roles = config.get("roles", {})
    settings = get_settings()
    
    # Setup Services
    qdrant = get_qdrant_client()
    embedding_provider = ProviderFactory.create_provider("openrouter", settings)
    completion_provider = ProviderFactory.create_provider("openrouter", settings) # Default
    
    rag = RAGChatService(qdrant, embedding_provider, completion_provider)
    
    question = "Was ist das Nullfeld?"
    print(f"❓ Question for all: '{question}'\n")
    
    results = []
    
    for role_id, role in roles.items():
        print(f"🎭 Testing {role['name']} ({role['model']})...")
        try:
            # Check model availability first? No, just try.
            
            res = rag.chat_with_context(
                question=question,
                character_name=role['name'],
                character_prompt=role['system_prompt'],
                lang="de",
                model_override=role.get("model")
            )
            
            answer = res['answer'][:100].replace('\n', ' ') + "..."
            sources = len(res.get('sources', []))
            model_used = res.get('model')
            
            status = "✅"
            if sources == 0:
                status = "⚠️ No Context"
            
            print(f"   ➜ Answer: {answer}")
            print(f"   ➜ Sources: {sources} | Model: {model_used} | {status}")
            
            results.append({
                "role": role_id,
                "status": "OK",
                "sources": sources,
                "model": model_used
            })
            
        except Exception as e:
            print(f"   ❌ Error: {e}")
            results.append({
                "role": role_id,
                "status": "ERROR",
                "error": str(e)
            })
        print("-" * 40)

    # Summary
    print("\n📊 Summary:")
    success = len([r for r in results if r['status'] == "OK"])
    print(f"Total Roles: {len(roles)}")
    print(f"Success: {success}")
    print(f"Failures: {len(results) - success}")
    
    if len(results) - success == 0:
        print("\n🎉 All roles passed!")
    else:
        print("\n💥 Some roles failed.")

if __name__ == "__main__":
    asyncio.run(test_roles())
