"""List available agents and their capabilities."""

from __future__ import annotations

from typing import TYPE_CHECKING

from core.pathways.models.tool import ToolOutput

if TYPE_CHECKING:
    from core.pathways.context import Context


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")

    runtime = context.services.runtime
    if not runtime:
        return ToolOutput(
            success=False,
            error="Runtime not available",
        )

    try:
        agents = await runtime.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}")
