"""Routing adapter helpers (minimal actor-first stub)."""

from __future__ import annotations

from typing import Any

from cleveragents.langgraph.nodes import Edge, NodeConfig, NodeType


def build_route_nodes_from_stream(
    name: str, operators: list[dict[str, Any]]
) -> tuple[dict[str, NodeConfig], list[Edge]]:
    nodes: dict[str, NodeConfig] = {
        "start": NodeConfig(name="start", type=NodeType.START)
    }
    edges: list[Edge] = []
    prev = "start"
    for i, op in enumerate(operators):
        node_name = f"op_{i}_{op.get('type', 'unknown')}"
        if op.get("type") == "map" and "agent" in op.get("params", {}):
            nodes[node_name] = NodeConfig(
                name=node_name, type=NodeType.AGENT, agent=op["params"]["agent"]
            )
        else:
            nodes[node_name] = NodeConfig(
                name=node_name, type=NodeType.FUNCTION, function=op.get("type")
            )
        edges.append(Edge(source=prev, target=node_name))
        prev = node_name
    edges.append(Edge(source=prev, target="end"))
    nodes["end"] = NodeConfig(name="end", type=NodeType.END)
    return nodes, edges
