"""Contextual relevancy metric — is the retrieved context relevant to the question?"""

from __future__ import annotations

from evalkit.judges.base import BaseJudge
from evalkit.metrics._text import format_context
from evalkit.metrics.base import BaseMetric
from evalkit.models import MetricScore, TestCase
from evalkit.prompts.contextual_relevancy import TEMPLATE


class ContextualRelevancyMetric(BaseMetric):
    """Measures whether the retrieved context is relevant to the question.

    A score of 1.0 means the context contains the information needed to answer
    the question.  A score of 0.0 means the context is entirely irrelevant to
    the question.

    Returns score 1.0 when context is empty (nothing to evaluate).
    """

    name: str = "contextual_relevancy"

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

    def score(self, test_case: TestCase, judge: BaseJudge) -> MetricScore:
        if not test_case.context:
            return MetricScore(
                metric_name=self.name,
                score=1.0,
                reason="No context provided — nothing to evaluate.",
                passed=True,
            )
        return super().score(test_case, judge)

    async def score_async(self, test_case: TestCase, judge: BaseJudge) -> MetricScore:
        if not test_case.context:
            return MetricScore(
                metric_name=self.name,
                score=1.0,
                reason="No context provided — nothing to evaluate.",
                passed=True,
            )
        return await super().score_async(test_case, judge)

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