"""List namespaces method for AsyncNamespaces."""

from __future__ import annotations

from typing import Any

import msgspec

from pinecone.models.namespaces.list import ListNamespacesResponse


_DECODER = msgspec.json.Decoder(ListNamespacesResponse)


async def list(
    self: Any,
    *,
    limit: int | None = None,
    pagination_token: str | None = None,
    prefix: str | None = None,
) -> ListNamespacesResponse:
    """List namespaces in the index.

    Args:
        limit (int | None): Maximum number of namespaces to return.
        pagination_token (str | None): Token to resume from a previous call.
        prefix (str | None): Filter namespaces by name prefix.

    Returns:
        ListNamespacesResponse with namespace descriptions and pagination info.

    Raises:
        PineconeApiException: If the API request fails.

    Examples:
        >>> response = await index.namespaces.list()
        >>> for ns in response.namespaces:
        ...     print(f"{ns.name}: {ns.record_count} records")
    """
    params: dict[str, Any] = {}
    if limit is not None:
        params["limit"] = limit
    if pagination_token is not None:
        params["paginationToken"] = pagination_token
    if prefix is not None:
        params["prefix"] = prefix

    data = await self._make_data_plane_request("GET", "/namespaces", params=params)
    return _DECODER.decode(data)
