# rag_service.py
import os
LOG_RAG = True  # toggle via env if you like
from openai import OpenAI
from rag_db import query_rag_context_from_db, build_rag_augmented_prompt

OPENAI_MODEL = "gpt-4o-mini"  # or your default

def generate_rag_addendum(user_query: str, tenant_id: int, top_k: int = 3) -> dict:
    """
    Returns dict:
    {
      'context': str,
      'matches': [ {title, similarity, content, rank, document_id} ],
      'addendum': str,
      'prompt': str (if return_prompt=True),
      'debug': { ... } (if debug=True)
    }
    """
    res = query_rag_context_from_db(user_query, tenant_id=tenant_id, top_k=top_k)
    context = res.get("context") or ""
    matches = res.get("matches", [])

    # Log retrieval stats
    if LOG_RAG:
        print(f"🧩 RAG: matches={len(matches)} top_k={top_k} tenant={tenant_id}", flush=True)
        for m in matches[:5]:
            print(f"   · [{m['rank']}] {m['title']}  sim={m['similarity']:.3f}", flush=True)
        print("🧩 RAG context preview:\n" + (context[:1200] or "(empty)"), flush=True)

    if not context.strip():
        return {"context": context, "matches": matches, "addendum": ""}

    prompt = build_rag_augmented_prompt(user_query, context)

    # Log exact prompt sent to the model
    if LOG_RAG:
        print("🧩 RAG prompt >>>", flush=True)
        print(prompt, flush=True)
        print("<<< END RAG prompt", flush=True)

    client = OpenAI()
    chat = client.chat.completions.create(
        model=OPENAI_MODEL,
        messages=[{"role": "user", "content": prompt}],
        temperature=0.2,
    )
    addendum = (chat.choices[0].message.content or "").strip()
    return {"context": context, "matches": matches, "addendum": addendum}