"""Quick smoke-test for Ollama before wiring it into the main bot."""
import asyncio
import httpx


async def test_ollama():
    print("1. Checking if Ollama is reachable at http://localhost:11434 ...")
    try:
        async with httpx.AsyncClient(timeout=5) as c:
            r = await c.get("http://localhost:11434/api/tags")
        if r.status_code == 200:
            models = [m["name"] for m in r.json().get("models", [])]
            print(f"   OK — models available: {models or '(none pulled yet)'}")
            if not any("llama3" in m for m in models):
                print("\n   WARNING: llama3:8b not pulled yet.")
                print("   Run in a separate terminal:  ollama pull llama3:8b")
                return
        else:
            print(f"   FAIL — unexpected status {r.status_code}")
            return
    except Exception as e:
        print(f"   FAIL — cannot reach Ollama: {e}")
        print("\n   Fix: open a new terminal and run:  ollama serve")
        return

    print("\n2. Testing chat completion (OpenAI-compat endpoint) ...")
    try:
        from openai import AsyncOpenAI

        client = AsyncOpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
        resp = await client.chat.completions.create(
            model="llama3:8b",
            messages=[{"role": "user", "content": "Reply with exactly: ollama ok"}],
            max_tokens=20,
            temperature=0,
        )
        reply = resp.choices[0].message.content.strip()
        print(f"   Model reply: {reply}")
        print("\n   PASS — Ollama is working. Safe to restart the bot.")
    except Exception as e:
        print(f"   FAIL — {e}")


asyncio.run(test_ollama())
