"""Response time evaluation rule."""

from agenteval.evaluation.rules.base import EvalRule, RuleResult, register_rule
from agenteval.models import Case, Turn


@register_rule
class ResponseTimeRule(EvalRule):
    """Check whether the reply latency is within the configured threshold."""

    name = "response_time"

    async def evaluate(self, case: Case, dialog: list[Turn]) -> RuleResult:
        if not dialog:
            return RuleResult(passed=False, reason="无回复记录")

        threshold_ms = self.params.get("max_ms")
        if threshold_ms is None:
            threshold_ms = case.expectations.response_time_max_ms
        if threshold_ms is None:
            return RuleResult(passed=True, reason="未配置响应时间阈值")

        last_turn = dialog[-1]
        latency = last_turn.latency_ms
        if latency is None:
            return RuleResult(passed=False, reason="无法获取响应时间")

        if latency > threshold_ms:
            return RuleResult(
                passed=False,
                score=max(0.0, 1.0 - (latency - threshold_ms) / threshold_ms),
                reason=f"响应时间 {latency}ms 超过阈值 {threshold_ms}ms",
            )

        score = 1.0 if latency <= 0 else min(1.0, threshold_ms / latency)
        return RuleResult(passed=True, score=score, reason=f"响应时间 {latency}ms 通过")
