"""Hallucination metric — does the answer introduce facts not in context?"""

from __future__ import annotations

from evalkit.metrics._text import format_context
from evalkit.metrics.base import BaseMetric
from evalkit.models import TestCase
from evalkit.prompts.hallucination import TEMPLATE


class HallucinationMetric(BaseMetric):
    """Measures whether the answer contains facts NOT present in the context.

    This metric is the inverse of faithfulness and uses a higher default
    threshold (0.7) because hallucination is a more serious failure mode.
    It focuses specifically on fabricated information — invented entities,
    false numbers, and made-up facts — as distinct from faithfulness which
    checks general grounding.

    A score of 1.0 means every factual claim can be verified in the context
    (no hallucination).  A score of 0.0 means the answer is full of facts not
    present in the context.
    """

    name: str = "hallucination"

    def __init__(self, threshold: float = 0.7) -> None:
        self.threshold = threshold

    def _build_prompt(self, test_case: TestCase) -> str:
        context_text = format_context(test_case.context)
        return TEMPLATE.format(
            question=test_case.question,
            context=context_text,
            answer=test_case.answer,
        )
