from __future__ import annotations

from app.modules.rag.intent_router_v2.models import EvidencePolicy


class EvidencePolicyFactory:
    def build(
        self,
        intent: str,
        *,
        sub_intent: str = "EXPLAIN",
        negations: list[str] | None = None,
        has_user_anchor: bool = True,
    ) -> EvidencePolicy:
        negations_set = set(negations or [])
        if intent == "CODE_QA":
            if sub_intent == "OPEN_FILE":
                return EvidencePolicy(require_def=False, require_flow=False, require_spec=False, allow_answer_without_evidence=False)
            if sub_intent == "EXPLAIN_LOCAL":
                return EvidencePolicy(require_def=True, require_flow=False, require_spec=False, allow_answer_without_evidence=False)
            if sub_intent == "TRACE_FLOW":
                return EvidencePolicy(require_def=True, require_flow=True, require_spec=False, allow_answer_without_evidence=False)
            if sub_intent == "ARCHITECTURE":
                return EvidencePolicy(require_def=True, require_flow=True, require_spec=False, allow_answer_without_evidence=False)
            if "tests" in negations_set and not has_user_anchor:
                return EvidencePolicy(require_def=True, require_flow=False, require_spec=False, allow_answer_without_evidence=False)
            return EvidencePolicy(require_def=True, require_flow=True, require_spec=False, allow_answer_without_evidence=False)
        if intent == "DOCS_QA":
            return EvidencePolicy(require_def=False, require_flow=False, require_spec=True, allow_answer_without_evidence=False)
        if intent == "GENERATE_DOCS_FROM_CODE":
            return EvidencePolicy(require_def=True, require_flow=False, require_spec=False, allow_answer_without_evidence=False)
        return EvidencePolicy(require_def=False, require_flow=False, require_spec=False, allow_answer_without_evidence=True)
