"""Base orchestrator — logical module boundary (not a separate service)."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any

from iuve_connect_agents.shared.goal_router import GoalRouteResult, OrchestratorType


@dataclass
class OrchestratorContext:
    """Inputs available when an orchestrator prepares a plan hint."""

    goal_text: str
    trust_state: str = ""
    device_platform: str = ""
    active_goal: str = ""
    world_state: dict[str, Any] = field(default_factory=dict)


@dataclass
class OrchestratorPlanHint:
    """Non-executing plan routing hint for downstream planner."""

    orchestrator: OrchestratorType
    planner_type: str | None
    session_preset: str | None
    execution_mode: str
    needs_planner: bool
    reasoning_summary: str
    metadata: dict[str, Any] = field(default_factory=dict)


class BaseOrchestrator:
    """Logical orchestrator — selects planner route, does not execute."""

    orchestrator_type: OrchestratorType
    planner_type: str | None = None
    session_preset: str | None = None
    execution_mode: str = "desktop"
    default_needs_planner: bool = True

    def describe(self) -> str:
        return f"{self.orchestrator_type.value} orchestrator"

    def plan_hint(self, ctx: OrchestratorContext, route: GoalRouteResult) -> OrchestratorPlanHint:
        needs_planner = self.default_needs_planner and not route.needs_clarification
        if route.needs_clarification:
            return OrchestratorPlanHint(
                orchestrator=self.orchestrator_type,
                planner_type=None,
                session_preset=None,
                execution_mode="chat_only",
                needs_planner=False,
                reasoning_summary="Goal unclear — ask user to clarify",
                metadata={"needs_clarification": True},
            )
        return OrchestratorPlanHint(
            orchestrator=self.orchestrator_type,
            planner_type=self.planner_type,
            session_preset=self.session_preset,
            execution_mode=self.execution_mode,
            needs_planner=needs_planner,
            reasoning_summary=f"Routed to {self.describe()}",
            metadata={"goal_category": route.primary_category.value},
        )
