"""Guardrail pipeline — providers, config, and testing."""

from __future__ import annotations

from typing import Any, Dict, Optional

from .._resource import AsyncResource, Resource


class Guardrails(Resource):
    """Sync guardrails operations."""

    def get_config(self) -> Dict[str, Any]:
        """Get current guardrails configuration."""
        return self._get("/v1/guardrails/config")

    def update_config(self, params: Dict[str, Any]) -> Dict[str, Any]:
        """Update guardrails configuration."""
        return self._put("/v1/guardrails/config", params)

    def list_providers(self) -> Dict[str, Any]:
        """List available guardrail providers."""
        return self._get("/v1/guardrails/providers")

    def add_provider(self, params: Dict[str, Any]) -> Dict[str, Any]:
        """Add a custom guardrail provider."""
        return self._post("/v1/guardrails/providers", params)

    def remove_provider(self, provider_id: str) -> Dict[str, Any]:
        """Remove a guardrail provider."""
        return self._delete(f"/v1/guardrails/providers/{provider_id}")

    def test(self, text: str, *, providers: Optional[list] = None) -> Dict[str, Any]:
        """Test content against guardrails."""
        body: Dict[str, Any] = {"text": text}
        if providers is not None:
            body["providers"] = providers
        return self._post("/v1/guardrails/test", body)

    def templates(self) -> Dict[str, Any]:
        """List available guardrail provider templates."""
        return self._get("/v1/guardrails/templates")


class AsyncGuardrails(AsyncResource):
    """Async guardrails operations."""

    async def get_config(self) -> Dict[str, Any]:
        return await self._get("/v1/guardrails/config")

    async def update_config(self, params: Dict[str, Any]) -> Dict[str, Any]:
        return await self._put("/v1/guardrails/config", params)

    async def list_providers(self) -> Dict[str, Any]:
        return await self._get("/v1/guardrails/providers")

    async def add_provider(self, params: Dict[str, Any]) -> Dict[str, Any]:
        return await self._post("/v1/guardrails/providers", params)

    async def remove_provider(self, provider_id: str) -> Dict[str, Any]:
        return await self._delete(f"/v1/guardrails/providers/{provider_id}")

    async def test(self, text: str, *, providers: Optional[list] = None) -> Dict[str, Any]:
        body: Dict[str, Any] = {"text": text}
        if providers is not None:
            body["providers"] = providers
        return await self._post("/v1/guardrails/test", body)

    async def templates(self) -> Dict[str, Any]:
        return await self._get("/v1/guardrails/templates")
