#!/usr/bin/env python3
"""Minimal RAGAS eval loop for WeKnora — see Ch20 and benchmark/README.md."""
from __future__ import annotations

import argparse
import json
import os
import sys
from pathlib import Path


def weknora_qa(url: str, key: str, question: str) -> dict:
    try:
        import httpx
    except ImportError:
        print("pip install httpx", file=sys.stderr)
        sys.exit(1)
    headers = {"Authorization": f"Bearer {key}"} if key else {}
    r = httpx.post(
        url,
        headers=headers,
        json={"query": question},
        timeout=120.0,
    )
    r.raise_for_status()
    data = r.json()
    citations = data.get("citations") or data.get("sources") or []
    contexts = []
    for c in citations:
        if isinstance(c, dict):
            contexts.append(c.get("text") or c.get("content") or str(c))
        else:
            contexts.append(str(c))
    return {
        "answer": data.get("answer") or data.get("message") or "",
        "contexts": contexts,
    }


def main() -> None:
    p = argparse.ArgumentParser(description="Run RAGAS on a WeKnora JSONL benchmark")
    p.add_argument("--benchmark", type=Path, required=True)
    p.add_argument("--out", type=Path, required=True)
    p.add_argument("--url", default=os.environ.get("WEKNORA_CHAT_URL", ""))
    p.add_argument("--key", default=os.environ.get("WEKNORA_KEY", ""))
    p.add_argument("--dry-run", action="store_true", help="Only validate JSONL schema")
    args = p.parse_args()

    rows_out = []
    for line in args.benchmark.read_text(encoding="utf-8").splitlines():
        if not line.strip():
            continue
        ex = json.loads(line)
        for field in ("id", "question", "ground_truth"):
            if field not in ex:
                raise SystemExit(f"Missing field {field} in {ex.get('id')}")
        if args.dry_run:
            rows_out.append({"id": ex["id"], "dry_run": True})
            continue
        if not args.url:
            raise SystemExit("Set WEKNORA_CHAT_URL or --url")
        out = weknora_qa(args.url, args.key, ex["question"])
        rows_out.append(
            {
                "id": ex["id"],
                "question": ex["question"],
                "ground_truth": ex["ground_truth"],
                "answer": out["answer"],
                "contexts": out["contexts"],
            }
        )

    args.out.parent.mkdir(parents=True, exist_ok=True)
    args.out.write_text(json.dumps(rows_out, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    print(f"Wrote {len(rows_out)} rows to {args.out}")
    if not args.dry_run:
        print("Next: pip install ragas datasets && run RAGAS metrics in notebook or extend this script.")


if __name__ == "__main__":
    main()
