#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "typer",
# ]
# ///
"""
List all available agent blueprints from Agent Orchestrator API.

Configuration:
    AGENT_ORCHESTRATOR_API_URL: API base URL (default: http://localhost:8765)
"""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent / "lib"))

import typer
from agent_api import list_agents_api, AgentAPIError

app = typer.Typer(add_completion=False)


@app.command()
def main(
    tags: str = typer.Option(
        "",
        "--tags", "-t",
        help="Comma-separated tags to filter agents. Agents must have ALL specified tags. Empty = all agents."
    )
):
    """
    List all available agent blueprints filtered by tags.

    Displays: agent name, description

    Examples:
        ao-list-blueprints                    # List all agents (no filter)
        ao-list-blueprints --tags internal    # List agents with 'internal' tag
        ao-list-blueprints --tags external    # List agents with 'external' tag
        ao-list-blueprints -t internal,jira   # List agents with BOTH tags
    """
    try:
        agents = list_agents_api(tags=tags if tags else None)
    except AgentAPIError as e:
        print(f"Error: {e}", file=sys.stderr)
        sys.exit(1)

    if not agents:
        filter_msg = f"with tags '{tags}'" if tags else ""
        print(f"No agent blueprints found {filter_msg}".strip())
        return

    # Display agents
    first = True
    for agent in agents:
        if first:
            first = False
        else:
            print("---")
            print()

        name = agent.get("name", "unknown")
        description = agent.get("description", "")

        print(f"{name}:")
        print(description)
        print()


if __name__ == "__main__":
    app()
