"""Robot Framework helper for tdd_actor_compiler_actor_ref_1429 tests.

Regression tests for issue #1429: the actor compiler must read actor_ref
from NodeDefinition.actor_ref (a typed Pydantic field), not from
node.config.get("actor_ref") (a raw dict lookup).

Two aspects are tested:
  subgraph-field  -- _map_node() sets NodeConfig.subgraph from node.actor_ref
  subgraph-refs   -- compile_actor() populates metadata.subgraph_refs from
                     node_def.actor_ref

Usage:
    python robot/helper_tdd_actor_compiler_actor_ref_1429.py subgraph-field
    python robot/helper_tdd_actor_compiler_actor_ref_1429.py subgraph-refs
"""

from __future__ import annotations

import sys
from pathlib import Path

_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

from cleveragents.actor.compiler import compile_actor  # noqa: E402
from cleveragents.actor.schema import (  # noqa: E402
    ActorConfigSchema,
    ActorType,
    EdgeDefinition,
    NodeDefinition,
    NodeType,
    RouteDefinition,
)

_ACTOR_REF = "local/code-reviewer"


def _make_outer_actor() -> ActorConfigSchema:
    """GRAPH actor with one AGENT node and one SUBGRAPH node referencing _ACTOR_REF."""
    nodes = [
        NodeDefinition(
            id="start",
            type=NodeType.AGENT,
            name="Start",
            description="Start node",
            config={"agent": "default"},
        ),
        NodeDefinition(
            id="sub",
            type=NodeType.SUBGRAPH,
            name="Sub",
            description="Subgraph node",
            config={},
            actor_ref=_ACTOR_REF,
        ),
    ]
    route = RouteDefinition(
        nodes=nodes,
        edges=[EdgeDefinition(from_node="start", to_node="sub")],
        entry_node="start",
        exit_nodes=["sub"],
    )
    return ActorConfigSchema(
        name="test/outer-1429",
        type=ActorType.GRAPH,
        description="Outer actor for #1429 regression test",
        provider="openai",
        model="gpt-4",
        route=route,
    )


def _make_inner_actor() -> ActorConfigSchema:
    """Simple leaf GRAPH actor with no subgraph nodes."""
    nodes = [
        NodeDefinition(
            id="leaf",
            type=NodeType.AGENT,
            name="Leaf",
            description="Leaf node",
            config={"agent": "default"},
        )
    ]
    route = RouteDefinition(
        nodes=nodes,
        edges=[],
        entry_node="leaf",
        exit_nodes=["leaf"],
    )
    return ActorConfigSchema(
        name=_ACTOR_REF,
        type=ActorType.GRAPH,
        description="Inner actor for #1429 regression test",
        provider="openai",
        model="gpt-4",
        route=route,
    )


def main() -> int:
    if len(sys.argv) < 2:
        print(
            "Usage: helper_tdd_actor_compiler_actor_ref_1429.py"
            " <subgraph-field|subgraph-refs>"
        )
        return 1

    command = sys.argv[1]
    outer = _make_outer_actor()
    inner = _make_inner_actor()
    registry = {outer.name: outer, inner.name: inner}

    if command == "subgraph-field":
        # Tests _map_node(): NodeConfig.subgraph must be set from node.actor_ref
        try:
            compiled = compile_actor(outer, actor_resolver=registry.get)
            node_cfg = compiled.nodes.get("sub")
            if node_cfg is None:
                print("tdd-1429-subgraph-field-fail: 'sub' not in compiled.nodes")
                return 1
            if node_cfg.subgraph != _ACTOR_REF:
                print(
                    f"tdd-1429-subgraph-field-fail: "
                    f"expected subgraph={_ACTOR_REF!r}, got {node_cfg.subgraph!r}"
                )
                return 1
            print(f"tdd-1429-subgraph-field-ok: subgraph={node_cfg.subgraph!r}")
            return 0
        except Exception as exc:
            print(f"tdd-1429-subgraph-field-error: {exc}")
            return 1

    if command == "subgraph-refs":
        # Tests compile_actor(): metadata.subgraph_refs must be populated
        # from node_def.actor_ref
        try:
            compiled = compile_actor(outer, actor_resolver=registry.get)
            refs = compiled.metadata.subgraph_refs
            if refs.get("sub") != _ACTOR_REF:
                print(
                    f"tdd-1429-subgraph-refs-fail: "
                    f"expected subgraph_refs['sub']={_ACTOR_REF!r}, got {refs!r}"
                )
                return 1
            print(f"tdd-1429-subgraph-refs-ok: subgraph_refs={refs!r}")
            return 0
        except Exception as exc:
            print(f"tdd-1429-subgraph-refs-error: {exc}")
            return 1

    print(f"Unknown command: {command}")
    return 1


if __name__ == "__main__":
    sys.exit(main())
