"""Async index management namespace implementation.

This module provides the async implementation for managing Pinecone indexes including
listing, creating, deleting, and describing indexes.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from pinecone.__interface__.indexes import AsyncIndexNamespaceProtocol
from pinecone._internal.adapters.indexes_adapter import IndexesAdapter
from pinecone._internal.introspection import InteractiveIntrospectionMixin


if TYPE_CHECKING:
    from pinecone._internal.http import AsyncHTTPClient
    from pinecone.async_indexes import (
        _configure,
        _create,
        _create_for_model,
        _delete,
        _describe,
        _helpers,
        _list,
    )


class AsyncIndexNamespace(InteractiveIntrospectionMixin, AsyncIndexNamespaceProtocol):
    """Async implementation of index management operations.

    This class provides async CRUD operations for managing Pinecone indexes, enabling
    concurrent index management in async workflows.

    Args:
        http_client: Async HTTP client for making API requests
        base_url: Base URL for the Index management API

    Example:
        >>> from pinecone import AsyncPinecone
        >>> import asyncio
        >>>
        >>> async def main():
        ...     async with AsyncPinecone(api_key="...") as pc:
        ...         # List all indexes
        ...         indexes = await pc.indexes.list()
        ...         async for index in indexes:
        ...             print(f"{index.name}: {index.config.dimension}D")
        ...
        ...         # Create an index
        ...         info = await pc.indexes.create(
        ...             name="my-index",
        ...             dimension=1536,
        ...             spec={"cloud": "aws", "region": "us-east-1"},
        ...         )
        ...         print(f"Created: {info.name}")
        >>>
        >>> asyncio.run(main())

    Example with concurrent operations:
        >>> async def create_multiple_indexes(specs):
        ...     async with AsyncPinecone(api_key="...") as pc:
        ...         tasks = [
        ...             pc.indexes.create(
        ...                 name=spec["name"], dimension=spec["dim"], spec=spec["spec"]
        ...             )
        ...             for spec in specs
        ...         ]
        ...         return await asyncio.gather(*tasks)
    """

    def __init__(
        self,
        http_client: AsyncHTTPClient,
        base_url: str = "https://api.pinecone.io",
    ):
        """Initialize AsyncIndexNamespace.

        Args:
            http_client: Async HTTP client for making API requests
            base_url: Base URL for the Index management API. Defaults to production endpoint.
        """
        self._http = http_client
        self._base_url = base_url
        self._adapter = IndexesAdapter()

        # Import and bind methods once at initialization (eager binding)
        from pinecone.async_indexes import (  # noqa: PLC0415
            _configure,
            _create,
            _create_for_model,
            _delete,
            _describe,
            _helpers,
            _list,
        )

        self._normalize_spec = _helpers._normalize_spec.__get__(self, type(self))
        self.list = _list.list.__get__(self, type(self))  # type: ignore[method-assign]
        self.create = _create.create.__get__(self, type(self))  # type: ignore[method-assign]
        self.delete = _delete.delete.__get__(self, type(self))  # type: ignore[method-assign]
        self.describe = _describe.describe.__get__(self, type(self))  # type: ignore[method-assign]
        self.configure = _configure.configure.__get__(self, type(self))  # type: ignore[method-assign]
        self.create_for_model = _create_for_model.create_for_model.__get__(self, type(self))  # type: ignore[method-assign]

    # Protocol method stubs (overridden in __init__)
    async def list(self, *args, **kwargs):  # type: ignore[no-untyped-def]
        """Stub for list method (overridden in __init__)."""
        raise NotImplementedError  # pragma: no cover

    async def create(self, *args, **kwargs):  # type: ignore[no-untyped-def]
        """Stub for create method (overridden in __init__)."""
        raise NotImplementedError  # pragma: no cover

    async def delete(self, *args, **kwargs):  # type: ignore[no-untyped-def]
        """Stub for delete method (overridden in __init__)."""
        raise NotImplementedError  # pragma: no cover

    async def describe(self, *args, **kwargs):  # type: ignore[no-untyped-def]
        """Stub for describe method (overridden in __init__)."""
        raise NotImplementedError  # pragma: no cover

    async def configure(self, *args, **kwargs):  # type: ignore[no-untyped-def]
        """Stub for configure method (overridden in __init__)."""
        raise NotImplementedError  # pragma: no cover

    async def create_for_model(self, *args, **kwargs):  # type: ignore[no-untyped-def]
        """Stub for create_for_model method (overridden in __init__)."""
        raise NotImplementedError  # pragma: no cover
