#!/usr/bin/env python3
"""
Test script to verify OpenRouter API is working correctly
"""

import asyncio
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))

from app.services.openrouter import openrouter_service


async def test_openrouter_connection():
    """Test basic OpenRouter API connection"""
    print("🔍 Testing OpenRouter API Connection...")
    print(f"Base URL: {openrouter_service.base_url}")
    print(f"Model: {openrouter_service.model}")
    print(f"API Key: {'✓ Set' if openrouter_service.api_key else '✗ Not Set'}")
    print()
    
    if not openrouter_service.api_key:
        print("❌ ERROR: OPENROUTER_API_KEY not set in environment")
        return False
    
    # Test with a simple trading plan generation
    test_prompt = """Analyze the current gold (XAU/USD) market conditions and provide a brief trading recommendation.

Current Price: $2,050.00
Market Context: Gold has been consolidating in a range between $2,030 and $2,060.

Respond in JSON format with:
{
    "recommendation": "BUY|SELL|HOLD",
    "confidence": 75,
    "reasoning": "Brief explanation"
}
"""
    
    try:
        print("📡 Sending test request to OpenRouter...")
        response = await openrouter_service.generate_trading_plan(test_prompt)
        print("✅ SUCCESS! OpenRouter API is working")
        print()
        print("Response received:")
        print("-" * 60)
        import json
        print(json.dumps(response, indent=2))
        print("-" * 60)
        return True
        
    except Exception as e:
        print(f"❌ ERROR: {str(e)}")
        import traceback
        traceback.print_exc()
        return False


if __name__ == "__main__":
    success = asyncio.run(test_openrouter_connection())
    sys.exit(0 if success else 1)
