"""ConsistencyCheck: Validate consistency within AI output."""

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 detecting inconsistencies and contradictions within text.

Analyze the output for:
- Internal contradictions (claims that contradict each other)
- Logical inconsistencies
- Factual contradictions
- Tonal or stylistic inconsistencies
- Contradictions with the conversation history (if provided)

Respond with a JSON object:
{
  "score": <float 0.0-1.0, where 1.0 means fully consistent>,
  "evidence": "<description of any inconsistencies found>"
}
"""


@register("consistency")
class ConsistencyCheck(BaseCheck):
    """Validate internal consistency of AI-generated output.

    Requires:
        - ctx.output: The text to analyze for consistency
        - ctx.conversation: Previous messages for multi-turn consistency (optional)

    Detects contradictions and logical inconsistencies within the output or
    relative to conversation history.

    Example:
        check = ConsistencyCheck(threshold=0.85)
    """

    requires_llm = True
    version = "1.0.0"
    description = "Validate internal consistency of AI output"

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

        Args:
            ctx: Context to validate.

        Raises:
            ContextValidationError: If output is missing.
        """
        if ctx.output is None:
            raise ContextValidationError(
                "ConsistencyCheck requires ctx.output to be set.\n"
                "Example: Context(output='The response')"
            )

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

        Args:
            ctx: Context with output to validate.

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

        provider = get_provider()

        # Build conversation context if available
        conversation_str = ""
        if ctx.conversation:
            lines = []
            for msg in ctx.conversation:
                role = msg.get("role", "unknown")
                content = msg.get("content", "")
                lines.append(f"{role}: {content}")
            conversation_str = "\n".join(lines) + "\n"

        prompt = f"""CONVERSATION HISTORY:
{conversation_str or 'None provided'}

CURRENT OUTPUT:
{ctx.output}

Analyze the output for internal contradictions, logical inconsistencies, and
contradictions with the conversation history. Score higher if the output is
consistent. Score lower if contradictions are found."""

        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,
        )
