"""Delete method for indexes namespace."""

from __future__ import annotations

from typing import Any

from pinecone._internal.constants import INDEXES_API_VERSION
from pinecone._internal.logging import get_logger
from pinecone._internal.validation import require_non_empty


logger = get_logger(__name__)


def delete(self: Any, name: str, *, timeout: int | None = None) -> None:
    """Delete an index.

    Warning: This operation is irreversible. The index and all its data
        will be permanently deleted.

    Args:
        name: Index name (string) to delete.
        timeout: Optional timeout in seconds. If None (default), uses the
            client's configured timeout. Increase for indexes that may take
            longer to delete (e.g., large pod-based indexes).

    Returns:
        None

    Raises:
        ValueError: If name is empty or timeout is not positive
        NotFoundError: If index does not exist
        APIError: If the API returns an error response (including 412 if there
            is a pending collection from this index)
        APIConnectionError: If the request fails to connect
        TimeoutError: If the request times out

    Example:
        >>> pc.indexes.delete("my-index")

    Example with timeout:
        >>> # Give extra time for large index deletion
        >>> pc.indexes.delete("large-index", timeout=120)

    Example with confirmation:
        >>> # Check before deleting
        >>> try:
        ...     info = pc.indexes.describe("my-index")
        ...     if info.config.dimension == 1536:
        ...         pc.indexes.delete("my-index")
        ... except NotFoundError:
        ...     print("Index doesn't exist")
    """
    require_non_empty("name", name)
    if timeout is not None and timeout <= 0:
        raise ValueError("timeout must be a positive integer")

    logger.info("Deleting index", index_name=name, timeout=timeout)

    try:
        self._http.delete(
            f"{self._base_url}/indexes/{name}",
            headers={
                "X-Pinecone-Api-Version": INDEXES_API_VERSION,
            },
            timeout=timeout,
        )

        logger.debug("Successfully deleted index", index_name=name)

    except Exception:
        logger.exception("Failed to delete index", index_name=name)
        raise
