"""Adapter to load actors into the reactive stream router and route bridge."""

from __future__ import annotations

from typing import Any

from cleveragents.reactive.route_bridge import RouteBridge
from cleveragents.reactive.stream_router import ReactiveStreamRouter


def register_registry_agents(
    stream_router: ReactiveStreamRouter, route_bridge: RouteBridge, actor_registry: Any
) -> None:
    """Register actors from ActorRegistry into the router/route bridge.

    This expects actor_registry to expose list_actors() and get_actor(name).
    Each actor is injected as a callable via process_message_sync if present;
    otherwise skips.

    Exceptions from actor_registry.list_actors() or route_bridge refresh are
    allowed to propagate to the caller per CONTRIBUTING.md fail-fast policy.
    """
    actors = actor_registry.list_actors()  # type: ignore[attr-defined]

    for actor in actors or []:
        # Prefer synchronous processing if available
        if hasattr(actor, "process_message_sync") or hasattr(actor, "process_message"):
            stream_router.register_agent(actor.name, actor)

    # RouteBridge caches agents as a dict; refresh it
    if hasattr(route_bridge, "agents"):
        route_bridge.agents = {a.name: a for a in actors}
