"""
BQAS Test Executor

Executes BQAS tests via API proxy.
"""

import json
from typing import Dict

import httpx

from ...models import TestFramework


async def run_bqas_tests(service_def: Dict) -> Dict:
    """Proxy zu BQAS API im Voice-Service"""
    test_type = "golden" if service_def["framework"] == TestFramework.BQAS_GOLDEN else "rag"

    try:
        async with httpx.AsyncClient(timeout=120.0) as client:
            response = await client.post(
                f"http://localhost:8091/api/v1/bqas/run/{test_type}",
            )
            if response.status_code == 200:
                data = response.json()
                metrics = data.get("metrics", {})
                return {
                    "passed": metrics.get("passed_tests", 0),
                    "failed": metrics.get("failed_tests", 0),
                    "total": metrics.get("total_tests", 0),
                    "output": json.dumps(data, indent=2)[:5000],
                    "failed_test_ids": [],
                }
    except Exception as e:
        pass

    # Fehler wenn API nicht erreichbar - KEINE Demo-Daten
    return {
        "passed": 0,
        "failed": 0,
        "total": 0,
        "output": f"BQAS API nicht erreichbar. Nutze docker exec fuer {test_type} Tests.",
        "failed_test_ids": [],
    }
