"""``albus info`` — workspace summary."""

from __future__ import annotations

import json
from typing import Annotated

import typer

from albusos.cli import app
from albusos.cli._helpers import err, load_workspace_or_exit, out


@app.command("info")
def info_cmd(
    root: Annotated[str, typer.Option("--root", "-r", help="Workspace root.")] = ".",
    json_output: Annotated[bool, typer.Option("--json", "-j", help="Emit JSON.")] = False,
) -> None:
    """Show a summary of the current workspace."""
    ws = load_workspace_or_exit(root)
    tool_count = ws.get_tool_count()

    if json_output:
        data = {
            "root": str(ws.root),
            "agents": sorted(ws.agents),
            "pathways": sorted(ws.pathways),
            "skills": sorted(ws.skills),
            "tool_count": tool_count,
        }
        out.print_json(json.dumps(data, indent=2))
        return

    err.print(f"\n[bold]Workspace:[/bold] {ws.root}\n")

    _section("Agents", ws.agents)
    _section("Pathways", ws.pathways)
    _section("Skills", ws.skills)

    err.print(f"  [bold]Tools:[/bold] {tool_count} registered\n")


def _section(label: str, items: list[str]) -> None:
    if items:
        err.print(f"  [bold]{label}:[/bold] {len(items)}")
        for item in sorted(items):
            err.print(f"    - {item}")
    else:
        err.print(f"  [bold]{label}:[/bold] (none)")
