#!/usr/bin/env python3
"""Adapter: gbrain-evals → aria-brain-search.

Implementiert gbrain's expected Query-Interface, ruft aber aria-brain-search.py.
Phase-2-Roadmap (KAR-187 Sandbox-Verify) — heute Skeleton, später aktivieren.
"""
from __future__ import annotations
import json
import subprocess
from typing import Any

SEARCH_CLI = "/root/aria/scripts/aria-brain-search.py"


def query(text: str, top_k: int = 5) -> list[dict[str, Any]]:
    """Aria-Side BrainBench-Bridge."""
    try:
        r = subprocess.run(
            ["python3", SEARCH_CLI, text, "--top", str(top_k), "--json"],
            capture_output=True, text=True, timeout=30,
        )
        if r.returncode != 0:
            return []
        data = json.loads(r.stdout)
        # gbrain-evals expects: [{"slug": str, "score": float}, ...]
        return [{"slug": h.get("path", ""), "score": h.get("score", 0)}
                for h in data[:top_k]]
    except Exception as e:
        print(f"adapter error: {e}")
        return []


if __name__ == "__main__":
    import sys
    q = sys.argv[1] if len(sys.argv) > 1 else "BMW"
    print(json.dumps(query(q, top_k=5), indent=2))
