"""
LiteLLM + Guard Dog — two common patterns.

Mode A — Guard Dog proxy *in front of* LiteLLM (recommended for central scanning):
  1. Run LiteLLM proxy: litellm --config your.yaml --port 4000
  2. Run Guard Dog gateway with upstream = LiteLLM:
       UPSTREAM_OPENAI_BASE=http://127.0.0.1:4000/v1
       OPENAI_API_KEY=<litellm master key or virtual key>
       python samples/proxy/gateway_multi_provider.py
  3. Point clients at Guard Dog (8090). Latency: client → scan → LiteLLM → model.

Mode B — Client calls LiteLLM directly; use a pre-call hook inside your app:
  - Import litellm and register input_callback / modify input before completion.
  - Call guard_dog.scanner.scan on assembled user text; abort if blocked.
  - Ordering: scan must run *before* the provider sees plaintext. Adds app-side CPU.

This file demonstrates Mode B with a minimal completion call after a local scan.
Install: Install the supplied Guard Dog wheel, then: pip install litellm

Environment:
  LITELLM_MODEL — e.g. gpt-4o-mini
  OPENAI_API_KEY — provider key (LiteLLM forwards to OpenAI unless you configure routers)
"""
from __future__ import annotations

import os
import sys

_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if _ROOT not in sys.path:
    sys.path.insert(0, _ROOT)

from guard_dog.scanner import ScanConfig, scan


def main() -> None:
    try:
        import litellm
    except ImportError:
        print("Install: pip install litellm")
        raise

    text = os.environ.get("LITELLM_USER_TEXT", "Say hello in two words.")
    cfg = ScanConfig.high_fidelity()
    r = scan(text, config=cfg)
    if r.risk_score >= float(os.environ.get("GUARD_DOG_GATEWAY_RISK_THRESHOLD", "0.5")) or not r.is_clean:
        print("Local scan blocked:", r.risk_score)
        sys.exit(2)

    model = os.environ.get("LITELLM_MODEL", "gpt-4o-mini")
    resp = litellm.completion(
        model=model,
        messages=[{"role": "user", "content": text}],
        max_tokens=32,
    )
    if isinstance(resp, dict):
        content = resp["choices"][0]["message"].get("content")
    else:
        content = resp.choices[0].message.content
    print(content)


if __name__ == "__main__":
    main()
