"""SPCS bridge — proxy advanced AI operations to SPCS-resident DataBridge.

For operations requiring GPU, large models, or Cortex functions,
this bridge proxies requests to a DataBridge instance running
inside Snowpark Container Services.

Reuses:
    - ``src.spcs.spec_generator`` for SPCS spec generation
    - ``src.cortex_agent.cortex_client`` for Cortex API calls
"""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from databridge_core.sdk.core.context import SDKContext
from databridge_core.sdk.core.exceptions import NotImplementedStubError, SDKError


class SPCSBridge:
    """Bridge to SPCS-resident DataBridge for advanced AI operations.

    Proxies operations that require server-side AI capabilities
    (detection, debate, self-learning, vector search) to a
    DataBridge instance running in Snowpark Container Services.

    Reuses:
        - ``src.spcs.spec_generator.SPCSSpecGenerator``
        - ``src.cortex_agent.cortex_client.CortexClient``

    Usage::

        bridge = SPCSBridge(ctx=ctx, endpoint="https://spcs-instance.snowflakecomputing.app")
        result = bridge.detect("data.csv", min_confidence=0.5)
        result = bridge.debate("Should we reclassify account 4100?")
    """

    def __init__(
        self,
        ctx: SDKContext,
        endpoint: Optional[str] = None,
    ):
        self._ctx = ctx
        self._endpoint = endpoint or ctx.config.spcs_endpoint
        if not self._endpoint:
            raise SDKError(
                "SPCS endpoint not configured. Set SDK_SPCS_ENDPOINT or "
                "pass endpoint= to SPCSBridge()."
            )

    def detect(
        self,
        file_path: str,
        min_confidence: float = 0.0,
        **kwargs: Any,
    ) -> Dict[str, Any]:
        """Run detection via SPCS endpoint.

        Proxies to the SPCS-resident ``detect_grounded`` tool.

        Reuse: HTTP POST to ``{endpoint}/tools/detect_grounded_file``
        """
        raise NotImplementedStubError(
            "SPCSBridge",
            "detect",
            "HTTP POST to SPCS endpoint /tools/detect_grounded_file",
        )

    def debate(
        self,
        question: str,
        context: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """Run a decision debate via SPCS endpoint.

        Proxies to the SPCS-resident debate engine.

        Reuse: HTTP POST to ``{endpoint}/tools/debate_decision``
        """
        raise NotImplementedStubError(
            "SPCSBridge",
            "debate",
            "HTTP POST to SPCS endpoint /tools/debate_decision",
        )

    def search_knowledge(
        self,
        query: str,
        max_results: int = 10,
    ) -> Dict[str, Any]:
        """Search knowledge base via SPCS-hosted vector store.

        Reuse: HTTP POST to ``{endpoint}/tools/search_knowledge``
        """
        raise NotImplementedStubError(
            "SPCSBridge",
            "search_knowledge",
            "HTTP POST to SPCS endpoint /tools/search_knowledge",
        )

    def self_learn(
        self,
        feedback: List[Dict[str, Any]],
    ) -> Dict[str, Any]:
        """Trigger self-learning cycle via SPCS endpoint.

        Proxies to the SPCS-resident feedback loop.

        Reuse: HTTP POST to ``{endpoint}/tools/evaluation_feedback_loop``
        """
        raise NotImplementedStubError(
            "SPCSBridge",
            "self_learn",
            "HTTP POST to SPCS endpoint /tools/evaluation_feedback_loop",
        )

    def health(self) -> Dict[str, Any]:
        """Check SPCS endpoint health.

        Reuse: HTTP GET ``{endpoint}/health``
        """
        raise NotImplementedStubError(
            "SPCSBridge",
            "health",
            "HTTP GET to SPCS endpoint /health",
        )
