"""Backups namespace implementation for managing serverless index backups.

This module provides the implementation for backup management operations including
creating, listing, describing, and deleting backups of serverless indexes.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from pinecone.__interface__.backups import (
    BackupInfoProtocol,
    BackupNamespaceProtocol,
)
from pinecone._internal.adapters.backups_adapter import BackupsAdapter
from pinecone._internal.constants import CONTROL_PLANE_API_VERSION
from pinecone._internal.introspection import InteractiveIntrospectionMixin
from pinecone._internal.logging import get_logger


if TYPE_CHECKING:
    from pinecone._internal.http import HTTPClient
    from pinecone.backups import _create, _delete, _describe, _list
    from pinecone.models.pagination import Paginator


__all__ = ["Backups"]

logger = get_logger(__name__)


class Backups(InteractiveIntrospectionMixin, BackupNamespaceProtocol):
    """Implementation of backup management operations.

    This class demonstrates the canonical patterns for SDK implementation:
    - Protocol satisfaction with runtime checking
    - HTTP client usage for all API calls (when implemented)
    - Comprehensive error handling with context
    - Structured logging at key operation points
    - Clear documentation with runnable examples

    Args:
        http_client: HTTP client for making API requests
        base_url: Base URL for the Control Plane API

    Example:
        >>> from pinecone import Pinecone
        >>> pc = Pinecone(api_key="...")
        >>> backups = pc.backups
        >>>
        >>> # Create a backup
        >>> backup = backups.create(
        ...     index_name="my-index",
        ...     name="example-backup",
        ...     description="Monthly backup of production index",
        ... )
        >>> print(f"Backup ID: {backup.backup_id}")
        >>>
        >>> # List all backups
        >>> for backup in backups.list():
        ...     print(f"{backup.backup_id}: {backup.status}")
        >>>
        >>> # Describe a backup
        >>> info = backups.describe("8c85e612-ed1c-4f97-9f8c-8194e07bcf71")
        >>> print(f"Records: {info.record_count}")
        >>>
        >>> # Delete a backup
        >>> backups.delete("8c85e612-ed1c-4f97-9f8c-8194e07bcf71")
    """

    def __init__(
        self,
        http_client: HTTPClient,
        base_url: str = "https://api.pinecone.io",
    ):
        """Initialize Backups namespace.

        Args:
            http_client: HTTP client for making API requests
            base_url: Base URL for the Control Plane API. Defaults to production endpoint.
        """
        self._http = http_client
        self._base_url = base_url
        self._adapter = BackupsAdapter()
        self._headers = {"X-Pinecone-Api-Version": CONTROL_PLANE_API_VERSION}

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

        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]

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

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

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

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

    def __repr__(self) -> str:
        """Return developer-friendly representation."""
        return f"Backups(base_url='{self._base_url}')"
