"""Inference namespace implementation for embeddings and reranking.

This module provides the reference implementation for the Inference API, demonstrating
all SDK patterns including HTTP client usage, error handling, logging, and documentation.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from pinecone.__interface__.inference import InferenceNamespaceProtocol
from pinecone._internal.adapters.inference_adapter import InferenceAdapter
from pinecone._internal.introspection import InteractiveIntrospectionMixin


if TYPE_CHECKING:
    from pinecone._internal.http import HTTPClient


class Inference(InteractiveIntrospectionMixin, InferenceNamespaceProtocol):
    """Implementation of inference operations for embeddings and reranking.

    This class demonstrates the canonical patterns for SDK implementation:
    - Protocol satisfaction with runtime checking
    - HTTP client usage for all API calls
    - 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 Inference API

    Example:
        >>> from pinecone import Pinecone
        >>> pc = Pinecone(api_key="...")
        >>> inference = pc.inference
        >>>
        >>> # Generate embeddings
        >>> response = inference.embed(
        ...     model="multilingual-e5-large",
        ...     texts=["Hello world", "Goodbye world"],
        ...     parameters={"input_type": "passage", "truncate": "END"},
        ... )
        >>> embeddings = response.embeddings
        >>>
        >>> # Rerank documents
        >>> response = inference.rerank(
        ...     model="bge-reranker-v2-m3",
        ...     query="What is machine learning?",
        ...     documents=["ML is a subset of AI", "Python is a language"],
        ...     top_n=1,
        ... )
        >>> top_doc = response.results[0]
    """

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

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

        # Import and bind methods once at initialization (eager binding)
        from pinecone.inference import _embed, _helpers, _list_models, _rerank  # noqa: PLC0415

        self._make_inference_request = _helpers._make_inference_request.__get__(self, type(self))
        self.list_models = _list_models.list_models.__get__(self, type(self))  # type: ignore[method-assign]
        self.embed = _embed.embed.__get__(self, type(self))  # type: ignore[method-assign]
        self.rerank = _rerank.rerank.__get__(self, type(self))  # type: ignore[method-assign]

    def __repr__(self) -> str:
        """Return string representation of inference namespace."""
        return f"Inference(base_url='{self._base_url}')"
