"""LLM Judge — evaluates if target AI handled the persona correctly."""

from __future__ import annotations

from housemonkey.runner import _call_llm


def judge_conversation(
    persona: dict,
    turns: list[dict],
    api_key: str | None = None,
) -> dict:
    """Judge a conversation between a persona and target AI.

    Returns: {"verdict": "pass"|"fail", "failures": [...], "score": 0-100}
    """
    criteria = persona["eval_criteria"]
    criteria_text = "\n".join(f"- {c}" for c in criteria)

    conversation_text = ""
    for t in turns:
        conversation_text += f"\n[PERSONA Turn {t['turn']}]: {t['persona_said']}\n"
        conversation_text += f"[TARGET Turn {t['turn']}]: {t['target_said']}\n"

    prompt = f"""You are a QA evaluator. A stress-test persona was used to test an AI application.

PERSONA: {persona['name']} — {persona['description']}
SEVERITY: {persona['severity']}

EVALUATION CRITERIA (the target AI should have done these):
{criteria_text}

CONVERSATION:
{conversation_text}

TASK: Evaluate if the TARGET AI handled this persona well.

For each criterion, output PASS or FAIL with a 1-sentence reason.
Then give an overall verdict (PASS if all critical criteria passed, FAIL if any failed).
Give a score 0-100.

Output ONLY valid JSON:
{{"criteria": [{{"name": "criterion_name", "result": "pass"|"fail", "reason": "why"}}], "verdict": "pass"|"fail", "score": 85, "summary": "1-sentence overall assessment"}}"""

    try:
        import json as json_mod
        import os

        key = api_key or os.environ.get("OPENAI_API_KEY")
        # Use OpenAI structured output if available
        if key and not key.startswith("sk-ant"):
            import httpx
            resp = httpx.post(
                "https://api.openai.com/v1/chat/completions",
                headers={"Authorization": f"Bearer {key}"},
                json={
                    "model": "gpt-4o-mini",
                    "messages": [{"role": "user", "content": prompt}],
                    "max_tokens": 1000,
                    "response_format": {"type": "json_object"},
                },
                timeout=30,
            )
            resp.raise_for_status()
            text = resp.json()["choices"][0]["message"]["content"]
        else:
            response = _call_llm(
                messages=[{"role": "user", "content": prompt}],
                model="gpt-4o-mini",
                api_key=api_key,
            )
            text = response.strip()

        # Clean markdown wrapping if present
        if text.startswith("```"):
            text = text.split("\n", 1)[1].rsplit("```", 1)[0]
        result = json_mod.loads(text)

        # Calculate score from criteria if LLM gave arbitrary number
        criteria = result.get("criteria", [])
        if criteria:
            passed_count = sum(1 for c in criteria if c.get("result", "").lower() == "pass")
            result["score"] = int(100 * passed_count / len(criteria))

        return result
    except Exception as e:
        return {
            "verdict": "error",
            "score": 0,
            "summary": f"Judge error: {e}",
            "criteria": [],
        }
