from __future__ import annotations

from app.modules.agent.task_runtime.models import TaskRuntimeContext, WorkflowExecutionResult
from app.modules.agent.task_runtime.workflows.base import TaskWorkflow


class TaskWorkflowDispatcher:
    def __init__(
        self,
        *,
        docs_qa: TaskWorkflow,
        general_qa: TaskWorkflow,
        docs_generation: TaskWorkflow,
        openapi: TaskWorkflow,
        fallback: TaskWorkflow,
    ) -> None:
        self._docs_qa = docs_qa
        self._general_qa = general_qa
        self._docs_generation = docs_generation
        self._openapi = openapi
        self._fallback = fallback

    def dispatch(self, ctx: TaskRuntimeContext) -> WorkflowExecutionResult:
        workflow = self._select(getattr(ctx.route_result, "intent", "FALLBACK"))
        return workflow.run(ctx)

    def _select(self, intent: str) -> TaskWorkflow:
        normalized = (intent or "FALLBACK").upper()
        if normalized == "DOCUMENTATION_EXPLAIN":
            return self._docs_qa
        if normalized == "GENERAL_QA":
            return self._general_qa
        if normalized == "OPENAPI_GENERATION":
            return self._openapi
        if normalized == "GENERATE_DOCS_FROM_CODE":
            return self._docs_generation
        return self._fallback
