"""RelevanceCheck: Assess relevance of AI output to the input query."""

from __future__ import annotations

from ai_qa.checks.registry import register
from ai_qa.core.check import BaseCheck
from ai_qa.core.context import Context
from ai_qa.core.result import CheckResult
from ai_qa.exceptions import ContextValidationError
from ai_qa.providers import get_provider

JUDGE_PROMPT = """You are an expert at evaluating relevance. Your job is to assess
how relevant an AI-generated output is to the given input/query.

Consider:
- Does the output address the main question or request in the input?
- Are the key points related to what was asked?
- Is there extraneous or off-topic information?
- Does the output follow the instructions given in the input?

Respond with a JSON object:
{
  "score": <float 0.0-1.0, where 1.0 means highly relevant and on-topic>,
  "evidence": "<explanation of relevance issues or strengths>"
}
"""


@register("relevance")
class RelevanceCheck(BaseCheck):
    """Assess relevance of AI-generated output to the input query.

    Requires:
        - ctx.output: The AI-generated response
        - ctx.input: The user's query or request

    Evaluates whether the output is relevant and addresses the input query.

    Example:
        check = RelevanceCheck(threshold=0.75)
    """

    requires_llm = True
    version = "1.0.0"
    description = "Assess relevance of AI output to the input query"

    def validate_context(self, ctx: Context) -> None:
        """Validate that output and input exist.

        Args:
            ctx: Context to validate.

        Raises:
            ContextValidationError: If output or input is missing.
        """
        if ctx.output is None:
            raise ContextValidationError(
                "RelevanceCheck requires ctx.output to be set.\n"
                "Example: Context(input='What is X?', output='X is...')"
            )
        if ctx.input is None:
            raise ContextValidationError(
                "RelevanceCheck requires ctx.input to be set.\n"
                "Example: Context(input='What is X?', output='X is...')"
            )

    def evaluate(self, ctx: Context) -> CheckResult:
        """Evaluate output relevance to input using LLM.

        Args:
            ctx: Context with output and input.

        Returns:
            CheckResult with relevance score and evidence.
        """
        self.validate_context(ctx)

        provider = get_provider()

        prompt = f"""INPUT/QUERY:
{ctx.input}

OUTPUT:
{ctx.output}

Assess how relevant the output is to the input. Higher scores if the output directly
addresses the query and is on-topic. Lower scores if the output is tangential or
ignores the query."""

        resp = provider.judge(system=JUDGE_PROMPT, user=prompt)

        return CheckResult(
            name=self.name,
            score=resp.get("score", 0.5),
            passed=resp.get("score", 0.5) >= self.threshold,
            threshold=self.threshold,
            evidence=resp.get("evidence", ""),
            cost_usd=resp.get("cost_usd", 0.0),
            tokens_used=resp.get("tokens", {}),
            check_version=self.version,
        )
