"""Delegating retrieval policy resolver for process v2."""

from __future__ import annotations

from collections.abc import Sequence

from app.core.agent.processes.v2.workflows.doc_explain_api_exposed.steps.retrieval.retrieval_policy import (
    DocExplainApiExposedRetrievalPolicy,
)
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.retrieval.retrieval_policy import (
    DocExplainFindFilesRetrievalPolicy,
)
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.retrieval.retrieval_policy import (
    DocExplainSummaryRetrievalPolicy,
)
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.retrieval.retrieval_policy import (
    GeneralQaSummaryRetrievalPolicy,
)
from app.core.agent.utils.process_v2.models import V2RouteResult
from app.core.agent.utils.process_v2.plan_resolver.base import WorkflowRetrievalPlanResolver
from app.core.rag.retrieval.session_retriever import RetrievalPlan


class V2RetrievalPolicyResolver:
    """Resolve retrieval plans by delegating to workflow-local resolvers."""

    def __init__(self, resolvers: Sequence[WorkflowRetrievalPlanResolver] | None = None) -> None:
        self._resolvers = list(resolvers or self._default_resolvers())

    def resolve(self, route: V2RouteResult) -> RetrievalPlan:
        for resolver in self._resolvers:
            if resolver.supports(route):
                return resolver.resolve(route)
        raise ValueError(f"No retrieval policy for route: {(route.routing_domain, route.intent, route.subintent)!r}")

    def _default_resolvers(self) -> tuple[WorkflowRetrievalPlanResolver, ...]:
        return (
            GeneralQaSummaryRetrievalPolicy(),
            DocExplainFindFilesRetrievalPolicy(),
            DocExplainApiExposedRetrievalPolicy(),
            DocExplainSummaryRetrievalPolicy(),
        )
