"""End-to-end test of retrieve_context with intent routing + reranking.

Avoids loading the full FastAPI app; replicates the chat-handler retrieval
call shape and prints classifier output + final ranked sources for each query.
"""

import os
import sys
from pathlib import Path

from dotenv import load_dotenv
load_dotenv(Path.home() / "aaronai" / ".env", override=True)

sys.path.insert(0, str(Path(__file__).parent))

# Stub anthropic so api.py import doesn't fail without the SDK loaded.
# We only need retrieve_context.
import types
sys.modules.setdefault("anthropic", types.ModuleType("anthropic"))
sys.modules["anthropic"].Anthropic = lambda **kw: None

# Same for whisper if present
if "faster_whisper" not in sys.modules:
    sys.modules["faster_whisper"] = types.ModuleType("faster_whisper")

import importlib.util
spec = importlib.util.spec_from_file_location("api", Path(__file__).parent / "api.py")
api = importlib.util.module_from_spec(spec)
# Don't execute the whole module (it starts FastAPI). Instead, exec only definitions.
# Easier: just import the functions we need by exec'ing the file but catching errors.
try:
    spec.loader.exec_module(api)
except Exception as e:
    print(f"(continuing despite api.py side-effect error: {e})")

retrieve_context = api.retrieve_context

QUERIES = [
    "write me a bio",
    "my professional bio",
    "Aaron Nelson CV consulting and design work",
    "FWN3D consulting",
    "syllabi I have taught",
    "philosophy of teaching",
    "Hudson Valley Additive Manufacturing Center",
    "Aaron Nelson is an artist and educator working in additive manufacturing",
]

for q in QUERIES:
    pieces, sources = retrieve_context(q)
    print(f"\n=== {q!r} ===")
    for i, src in enumerate(sources, 1):
        print(f"  {i}. {src}")
