from __future__ import annotations

from dataclasses import asdict

from app.core.agent.processes.v2 import V2IntentRouter
from app.core.agent.utils.process_v2.plan_resolver import V2RetrievalPolicyResolver
from tests.pipeline_setup_v4.core.models import ExecutionPayload, V4Case
from tests.pipeline_setup_v4.executors.process_v2_router_executor import _KeywordLlm


class ProcessV2RouterPlusPolicyExecutor:
    def __init__(self) -> None:
        self._router = V2IntentRouter(llm=_KeywordLlm(), enable_llm_disambiguation=True)
        self._resolver = V2RetrievalPolicyResolver()

    def execute(self, case: V4Case) -> ExecutionPayload:
        route = self._router.route(case.query)
        plan = self._resolver.resolve(route)
        route_dump = asdict(route)
        actual = {
            "domain": route.routing_domain,
            "intent": route.intent,
            "sub_intent": route.subintent,
            "routing_mode": route.routing_mode,
            "llm_router_used": route.llm_router_used,
            "confidence": route.confidence,
            "profile": plan.profile,
            "layers": list(plan.layers),
            "limit": plan.limit,
            "filters": dict(plan.filters),
            "route": {
                "routing_domain": route.routing_domain,
                "intent": route.intent,
                "subintent": route.subintent,
                "target_terms": list(route.target_terms),
                "anchors": route_dump.get("anchors") or {},
            },
            "retrieval_plan": {
                "profile": plan.profile,
                "layers": list(plan.layers),
                "limit": plan.limit,
                "filters": dict(plan.filters),
            },
        }
        details = {
            "query": case.query,
            "route": route_dump,
            "plan": {
                "profile": plan.profile,
                "layers": list(plan.layers),
                "limit": plan.limit,
                "filters": dict(plan.filters),
            },
            "pipeline_steps": [
                {
                    "step": "intent_router",
                    "input": {"query": case.query},
                    "output": {
                        "domain": route.routing_domain,
                        "intent": route.intent,
                        "sub_intent": route.subintent,
                        "reason_short": route.reason_short,
                        "target_terms": list(route.target_terms),
                        "anchors": route_dump.get("anchors") or {},
                    },
                },
                {
                    "step": "retrieval_policy_resolver",
                    "input": {"route": route_dump},
                    "output": {
                        "profile": plan.profile,
                        "layers": list(plan.layers),
                        "limit": plan.limit,
                        "filters": dict(plan.filters),
                    },
                },
            ],
        }
        return ExecutionPayload(actual=actual, details=details)
