#!/usr/bin/env python
"""
Test script for OpenRouter model configuration with corrected endpoint.
"""

import asyncio
import logging
import os
from report.report_synthesis import ReportSynthesizer

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

async def test_openrouter_model():
    """Test OpenRouter model configuration with corrected endpoint."""
    logger.info("Testing OpenRouter model configuration with corrected endpoint...")
    
    # Create a custom model config with the corrected endpoint
    model_name = "openrouter-claude-3.7-sonnet"
    model_config = {
        "provider": "openrouter",
        "model_name": "anthropic/claude-3.7-sonnet",
        "temperature": 0.5,
        "max_tokens": 2048,
        "top_p": 1.0,
        "endpoint": "https://openrouter.ai/api/v1"  # Corrected endpoint
    }
    
    # We need to modify the config directly since ReportSynthesizer doesn't accept model_config
    # Import the config module
    from config.config import get_config
    
    # Get the config instance
    config = get_config()
    
    # Save the original config to restore later
    original_config = None
    if model_name in config.config_data.get('models', {}):
        original_config = config.config_data['models'][model_name].copy()
    
    # Update with corrected endpoint
    if 'models' not in config.config_data:
        config.config_data['models'] = {}
        
    config.config_data['models'][model_name] = model_config
    
    # Create a synthesizer with the model name
    synthesizer = ReportSynthesizer(model_name=model_name)
    
    # Print model configuration
    logger.info(f"Using model: {synthesizer.model_name}")
    logger.info(f"Model config: {synthesizer.model_config}")
    
    # Create a simple test message
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, can you help me with a test?"}
    ]
    
    try:
        # Generate completion
        logger.info("Generating completion...")
        response = await synthesizer.generate_completion(messages)
        
        # Print response
        logger.info(f"Response: {response}")
        
        return True
    except Exception as e:
        logger.error(f"Error testing OpenRouter model: {e}")
        return False

async def main():
    """Main function."""
    success = await test_openrouter_model()
    
    if success:
        logger.info("OpenRouter model test successful!")
    else:
        logger.error("OpenRouter model test failed!")

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