"""Registry of logical orchestrators."""

from __future__ import annotations

from iuve_connect_agents.shared.goal_router import GoalRouteResult, OrchestratorType, route_goal
from iuve_connect_agents.shared.orchestrators.base import (
    BaseOrchestrator,
    OrchestratorContext,
    OrchestratorPlanHint,
)


class _ChatOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.CHAT
    execution_mode = "chat_only"
    default_needs_planner = False


class _ResearchOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.RESEARCH
    session_preset = "web"
    execution_mode = "chat_only"
    default_needs_planner = False


class _DocumentOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.DOCUMENT
    planner_type = "document"
    session_preset = "files"


class _DeveloperOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.DEVELOPER
    planner_type = "coding"
    session_preset = "coding"


class _BrowserOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.BROWSER
    planner_type = "web"
    session_preset = "web"


class _DesktopOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.DESKTOP
    planner_type = "device"
    session_preset = "desktop"


class _FileOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.FILE
    planner_type = "document"
    session_preset = "files"


class _VisionOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.VISION
    planner_type = "device"
    session_preset = "web"


class _MarketplaceOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.MARKETPLACE
    planner_type = "artifact"
    execution_mode = "chat_only"
    default_needs_planner = False


class _WidgetOrchestrator(BaseOrchestrator):
    orchestrator_type = OrchestratorType.WIDGET
    planner_type = "web"
    session_preset = "web"
    execution_mode = "chat_only"
    default_needs_planner = False


ORCHESTRATOR_REGISTRY: dict[OrchestratorType, BaseOrchestrator] = {
    OrchestratorType.CHAT: _ChatOrchestrator(),
    OrchestratorType.RESEARCH: _ResearchOrchestrator(),
    OrchestratorType.DOCUMENT: _DocumentOrchestrator(),
    OrchestratorType.DEVELOPER: _DeveloperOrchestrator(),
    OrchestratorType.BROWSER: _BrowserOrchestrator(),
    OrchestratorType.DESKTOP: _DesktopOrchestrator(),
    OrchestratorType.FILE: _FileOrchestrator(),
    OrchestratorType.VISION: _VisionOrchestrator(),
    OrchestratorType.MARKETPLACE: _MarketplaceOrchestrator(),
    OrchestratorType.WIDGET: _WidgetOrchestrator(),
}


def get_orchestrator(orchestrator_type: OrchestratorType) -> BaseOrchestrator:
    return ORCHESTRATOR_REGISTRY[orchestrator_type]


def route_goal_to_plan_hint(
    goal_text: str,
    *,
    ctx: OrchestratorContext | None = None,
) -> tuple[GoalRouteResult, OrchestratorPlanHint, list[OrchestratorPlanHint]]:
    """Full Goal Router → orchestrator selection (primary + secondary hints)."""
    route = route_goal(goal_text)
    context = ctx or OrchestratorContext(goal_text=goal_text)
    primary = get_orchestrator(route.primary_orchestrator)
    primary_hint = primary.plan_hint(context, route)
    secondary_hints = [
        get_orchestrator(orch).plan_hint(context, route)
        for orch in route.secondary_orchestrators
    ]
    return route, primary_hint, secondary_hints
