"""Pure graph utilities (minimal actor-first stub)."""

from __future__ import annotations

from collections.abc import Callable
from typing import Any

from pydantic import BaseModel

from cleveragents.langgraph.nodes import Edge, NodeConfig


class PureGraph(BaseModel):
    name: str
    nodes: dict[str, NodeConfig]
    edges: list[Edge]

    def topological_order(self) -> list[str]:
        # Simple linear order based on insertion; not full topo sort
        order: list[str] = [n for n in self.nodes if n not in {"start", "end"}]
        return ["start", *order, "end"]

    def execute(
        self, fn_registry: dict[str, Callable[[Any], Any]], initial: Any = None
    ) -> Any:
        value = initial
        for node_name in self.topological_order():
            if node_name in ("start", "end"):
                continue
            node = self.nodes[node_name]
            if node.function and node.function in fn_registry:
                value = fn_registry[node.function](value)
        return value
