from __future__ import annotations

from app.modules.agent.code_qa_runtime.models import CodeQaValidationResult


class CodeQaPostEvidenceGate:
    def validate(
        self,
        *,
        answer: str,
        answer_mode: str,
        degraded_message: str,
    ) -> CodeQaValidationResult:
        normalized = (answer or "").strip()
        if not normalized:
            return CodeQaValidationResult(passed=False, action="repair", reasons=["empty_answer"])
        if answer_mode in {"degraded", "insufficient"} and "недостат" not in normalized.lower():
            return CodeQaValidationResult(passed=False, action="repair", reasons=["degraded_answer_missing_guardrail"])
        if answer_mode == "not_found" and "не найден" not in normalized.lower():
            return CodeQaValidationResult(passed=False, action="repair", reasons=["not_found_answer_missing_phrase"])
        if degraded_message and answer_mode != "normal" and len(normalized) < 24:
            return CodeQaValidationResult(passed=False, action="repair", reasons=["answer_too_short"])
        return CodeQaValidationResult(passed=True, action="return")
