from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from common import read_json
from rag_profiles import detect_rag_profile


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_CASES = ROOT / "plugins" / "1c" / "rag" / "profile-routing-smoke.json"


def main() -> int:
    parser = argparse.ArgumentParser(description="Check 1C RAG auto profile routing.")
    parser.add_argument("--cases", type=Path, default=DEFAULT_CASES)
    parser.add_argument("--print", action="store_true")
    args = parser.parse_args()

    data = read_json(args.cases)
    cases = data.get("cases")
    if not isinstance(cases, list):
        print(f"{args.cases}: cases must be a list", file=sys.stderr)
        return 1

    results = []
    for case in cases:
        query = str(case.get("query") or "")
        expected = str(case.get("expected_profile") or "")
        actual = detect_rag_profile(query)
        results.append(
            {
                "id": case.get("id"),
                "query": query,
                "expected_profile": expected,
                "actual_profile": actual,
                "status": "passed" if actual == expected else "failed",
            }
        )

    report = {
        "status": "ok" if all(result["status"] == "passed" for result in results) else "failed",
        "case_count": len(results),
        "results": results,
    }
    if args.print:
        print(json.dumps(report, ensure_ascii=False, indent=2))
    else:
        print(f"1C RAG profile routing status: {report['status']}")
    if report["status"] != "ok":
        for result in results:
            if result["status"] != "passed":
                print(
                    f"- {result['id']}: expected {result['expected_profile']}, got {result['actual_profile']}",
                    file=sys.stderr,
                )
    return 0 if report["status"] == "ok" else 1


if __name__ == "__main__":
    raise SystemExit(main())
