"""Faithfulness metric — is the answer supported by 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.faithfulness import TEMPLATE


class FaithfulnessMetric(BaseMetric):
    """Measures whether every claim in the answer is supported by the retrieved context.

    A score of 1.0 means the answer is fully grounded in the context (no
    hallucinations relative to context).  A score of 0.0 means the answer
    contains claims not found anywhere in the context.
    """

    name: str = "faithfulness"

    def __init__(self, threshold: float = 0.5) -> 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,
        )
