import asyncio
import sys

import httpx

from app.core.settings import get_settings

if sys.stdout and hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8")


async def check_models():
    settings = get_settings()
    api_key = settings.openrouter_api_key
    print(f"OpenRouter API Key: {api_key[:10]}...")

    headers = {
        "Authorization": f"Bearer {api_key}",
        "HTTP-Referer": "https://github.com/dscons",
        "X-Title": "DSCons Operations",
    }

    test_models = [
        "google/gemini-2.0-flash-exp:free",
        "google/gemini-2.0-flash-lite-preview-02-05:free",
        "google/gemini-flash-1.5-8b:free",
        "meta-llama/llama-3.3-70b-instruct:free",
        "deepseek/deepseek-chat:free",
        "deepseek/deepseek-r1:free",
        "stealth/ox-alpha",
    ]

    async with httpx.AsyncClient(timeout=15) as client:
        for model in test_models:
            payload = {
                "model": model,
                "messages": [{"role": "user", "content": "Ping"}],
                "max_tokens": 10,
            }
            try:
                res = await client.post(
                    "https://openrouter.ai/api/v1/chat/completions",
                    json=payload,
                    headers=headers,
                )
                print(f"Model {model}: HTTP {res.status_code}")
                if res.status_code == 200:
                    print(
                        "   Result:",
                        res.json()["choices"][0]["message"]["content"][:50],
                    )
                else:
                    print("   Error:", res.text[:100])
            except Exception as e:
                print(f"Model {model}: Exception {e}")


if __name__ == "__main__":
    asyncio.run(check_models())
