"""Tag tools for the MCP server."""

import httpx

from personal_assistant_mcp.client import AuthClient


async def list_tags(client: AuthClient, tag_type: str | None = None) -> list[dict]:
    """List tags, areas, or projects.

    Args:
        client: Authenticated API client.
        tag_type: Optional filter — "area", "project", or "tag".

    Returns:
        List of tag dicts with id, name, type, parent_id.
    """
    try:
        params = {}
        if tag_type:
            params["type"] = tag_type

        resp = await client.get("/api/tags", params=params)
        tags = resp.json()

        return [
            {
                "id": str(t["id"]),
                "name": t["name"],
                "type": t["type"],
                "parent_id": str(t["parent_id"]) if t.get("parent_id") else None,
            }
            for t in tags
        ]
    except httpx.HTTPStatusError as e:
        return [{"error": f"API error: {e.response.status_code} {e.response.text}"}]
    except httpx.HTTPError as e:
        return [{"error": f"Connection error: {e}"}]
