"""Async fetch-by-metadata method for AsyncIndex."""

from __future__ import annotations

from typing import Any

import msgspec

from pinecone.models.index.fetch_by_metadata import FetchByMetadataResponse


_DECODER = msgspec.json.Decoder(FetchByMetadataResponse)


async def fetch_by_metadata(
    self: Any,
    *,
    filter: dict[str, Any] | None = None,
    namespace: str = "",
    limit: int | None = None,
    pagination_token: str | None = None,
) -> FetchByMetadataResponse:
    """Fetch vectors matching a metadata filter.

    Args:
        filter: Metadata filter expression.
        namespace: Namespace to fetch from.
        limit: Maximum number of vectors to return.
        pagination_token: Token to resume from a previous call.

    Returns:
        FetchByMetadataResponse with matching vectors.

    Example:
        >>> response = await index.fetch_by_metadata(filter={"genre": "comedy"})
    """
    body: dict[str, Any] = {}
    if filter is not None:
        body["filter"] = filter
    if namespace:
        body["namespace"] = namespace
    if limit is not None:
        body["limit"] = limit
    if pagination_token is not None:
        body["paginationToken"] = pagination_token

    data = await self._make_data_plane_request("POST", "/vectors/fetch_by_metadata", body=body)
    return _DECODER.decode(data)
