#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test script for a single model from OpenRouter
"""

import asyncio
import aiohttp
import os
import json

# Get API key from environment or .env file
api_key = os.getenv('OPENROUTER_API_KEY')
if not api_key:
    with open(".env", "r") as f:
        for line in f:
            line = line.strip()
            if line.startswith("sk-or-v1-"):
                api_key = line
                break

if not api_key:
    print("❌ No API key found")
    exit(1)

async def test_model(model_name):
    """Test a single model"""
    # Remove :free tag for API call
    clean_model = model_name.split(":")[0]
    
    print(f"🚀 Testing model: {model_name}")
    print(f"   Clean name: {clean_model}")
    
    url = "https://openrouter.ai/api/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    payload = {
        "model": clean_model,
        "messages": [
            {"role": "user", "content": "Explain the concept of 'working hard' in one short sentence."}
        ],
        "max_tokens": 100
    }
    
    print(f"   Payload: {json.dumps(payload, indent=2)}")
    
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=headers, json=payload, timeout=30) as response:
                print(f"   Status: {response.status}")
                
                # Print response headers
                print("   Response Headers:")
                for key, value in response.headers.items():
                    print(f"     {key}: {value}")
                
                if response.status == 200:
                    result = await response.json()
                    print(f"   Full response: {json.dumps(result, indent=2)}")
                    
                    content = result.get('choices', [{}])[0].get('message', {}).get('content', '')
                    print(f"✅ Success - Content: '{content}'")
                    return True
                else:
                    error_text = await response.text()
                    print(f"❌ Status {response.status}: {error_text}")
                    return False
    except Exception as e:
        print(f"💥 Exception: {str(e)}")
        return False

async def main():
    """Main function"""
    model_to_test = "openai/gpt-oss-20b:free"
    await test_model(model_to_test)

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