"""LLM-powered design assistant for the SDK.

Reuses:
    - ``src.llm.get_llm_backend()`` for LLM access
    - ``src.agents.planner_agent.PlannerAgent`` for workflow planning
    - ``src.agents.cost_forecast.forecast()`` for cost estimation
"""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from databridge_core.sdk.core.context import SDKContext
from databridge_core.sdk.core.exceptions import NotImplementedStubError, SDKError
from databridge_core.sdk.core.types import WorkflowPlan
from databridge_core.sdk.workflows.registry import WorkflowRegistry


class SDKAssistant:
    """LLM-powered assistant that helps design SDK workflows.

    Takes natural-language requests and produces ``WorkflowPlan``
    objects recommending which workflows to run and with what parameters.

    Reuses:
        - ``src.llm.get_llm_backend()`` — LLM router
        - ``src.agents.planner_agent.PlannerAgent.plan_workflow()``
        - ``src.agents.cost_forecast.forecast()``
        - ``src.agents.voi.compute_voi()`` — Value of Information

    Usage::

        assistant = SDKAssistant(ctx=ctx, registry=registry)
        plan = assistant.design("reconcile GL against TB for Q4 close")
        # plan.workflow = "reconciliation.hash_compare"
        # plan.parameters = {"source_a_path": ..., ...}
    """

    def __init__(
        self,
        ctx: SDKContext,
        registry: WorkflowRegistry,
    ):
        self._ctx = ctx
        self._registry = registry

    def design(
        self,
        user_request: str,
        constraints: Optional[Dict[str, Any]] = None,
    ) -> WorkflowPlan:
        """Design a workflow plan from a natural-language request.

        Implementation should:
            1. Get available workflows from registry
            2. Use ``PlannerAgent.plan_workflow()`` with workflow catalog
            3. Map planned steps to SDK workflow names
            4. Optionally call ``forecast()`` for cost estimation
            5. Return ``WorkflowPlan``

        Reuse:
            - ``src.agents.planner_agent.PlannerAgent.plan_workflow(user_request)``
            - ``src.agents.cost_forecast.forecast(plan)``

        Args:
            user_request: Natural-language description.
            constraints: Optional constraints (budget, target DB, etc.).

        Returns:
            WorkflowPlan with recommended workflow and parameters.
        """
        # REUSE: PlannerAgent(config, available_agents, tool_registry=registry)
        #        .plan_workflow(user_request)
        # Then map PlannedStep -> SDK workflow name
        # Optionally: src.agents.cost_forecast.forecast(plan)
        raise NotImplementedStubError(
            "SDKAssistant",
            "design",
            "src.agents.planner_agent.PlannerAgent.plan_workflow(user_request) + "
            "src.agents.cost_forecast.forecast(plan)",
        )

    def suggest_next(
        self,
        completed_workflows: List[str],
        context: Optional[Dict[str, Any]] = None,
    ) -> List[WorkflowPlan]:
        """Suggest next workflows based on completed ones.

        Uses the planner's cost model and VOI engine to recommend
        the highest-value next steps.

        Reuse:
            - ``src.agents.voi.compute_voi()``
            - ``src.agents.bandit.select_strategy()``
        """
        raise NotImplementedStubError(
            "SDKAssistant",
            "suggest_next",
            "src.agents.voi.compute_voi() + src.agents.bandit.select_strategy()",
        )
