"""List available agents and their capabilities.

Requires an AgentRepository to be wired into the service container.
Consumer repos configure this when building their transport layer.
"""

from core.pathways.context import Context
from core.pathways.models.tool import ToolOutput


async def run(
    context: Context | None = None,
) -> ToolOutput:
    """List all registered agents.

    Args:
        context: Execution context (injected by framework)
    """
    if not context:
        return ToolOutput(success=False, error="Context not available")

    agent_repo = getattr(context.services, "agent_repo", None)
    if not agent_repo:
        return ToolOutput(
            success=False,
            error="AgentRepository not configured in ServiceContainer",
        )

    try:
        agents = await agent_repo.list()
        agent_list = []
        for agent in agents:
            defn = agent.definition
            agent_list.append(
                {
                    "id": defn.id,
                    "name": defn.name,
                    "handoff_description": defn.handoff_description,
                    "pathway": defn.pathway,
                    "model": defn.model,
                },
            )

        return ToolOutput(
            success=True,
            data={"agents": agent_list, "count": len(agent_list)},
        )
    except Exception as e:
        return ToolOutput(success=False, error=f"Failed to list agents: {e}")
