"""Ollama VM123 + fallback — Spec 029 T1."""
from __future__ import annotations
import os
import httpx

OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://10.10.10.123:11434").rstrip("/")
AGENTIC_LLM_MODEL = os.getenv("AGENTIC_LLM_MODEL", "qwen2.5:7b-instruct")
AGENTIC_LLM_ENABLED = os.getenv("AGENTIC_LLM_ENABLED", "false").lower() in ("1", "true", "yes")

def ollama_available() -> bool:
    try:
        with httpx.Client(timeout=3.0) as c:
            return c.get(f"{OLLAMA_BASE_URL}/api/tags").status_code == 200
    except Exception:
        return False

def advise_human_action(*, finding_title: str, finding_detail: str, kb_snippets: list[str] | None = None) -> tuple[str, str]:
    prompt = (
        "Advisor Agentic Ops Ligbox. Português BR, máx 6 frases. O que fazer AGORA?\n"
        f"Problema: {finding_title}\nDetalhe: {finding_detail}\nKB: {'---'.join(kb_snippets or [])[:2500] or 'N/A'}"
    )
    if not AGENTIC_LLM_ENABLED:
        return (f"Investigar manualmente: {finding_title}", "t0")
    if ollama_available():
        try:
            with httpx.Client(timeout=90.0) as c:
                r = c.post(f"{OLLAMA_BASE_URL}/api/chat", json={
                    "model": AGENTIC_LLM_MODEL,
                    "messages": [{"role": "user", "content": prompt}],
                    "stream": False,
                })
            if r.status_code == 200:
                txt = (r.json().get("message") or {}).get("content", "").strip()
                if txt:
                    return txt, AGENTIC_LLM_MODEL
        except Exception:
            pass
    return (f"Rever logs e specs para: {finding_title}", "t0-fallback")
