"""Provider key management (BYOK)."""

from __future__ import annotations

from typing import Any, Dict

from .._resource import AsyncResource, Resource


class Providers(Resource):
    """Sync provider key management."""

    def list(self) -> Dict[str, Any]:
        """List configured providers for the authenticated tenant."""
        return self._get("/v1/providers")

    def catalog(self) -> Dict[str, Any]:
        """List all supported providers from the catalog."""
        return self._get("/v1/providers/catalog")

    def register(self, provider: str, *, api_key: str) -> Dict[str, Any]:
        """Store a provider API key (BYOK, encrypted at rest)."""
        return self._post("/v1/providers", {"provider": provider, "api_key": api_key})

    def test(self, provider: str, api_key: str) -> Dict[str, Any]:
        """Validate a provider API key with a live request."""
        return self._post("/v1/providers/test", {"provider": provider, "api_key": api_key})

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


class AsyncProviders(AsyncResource):
    """Async provider key management."""

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

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

    async def register(self, provider: str, *, api_key: str) -> Dict[str, Any]:
        return await self._post("/v1/providers", {"provider": provider, "api_key": api_key})

    async def test(self, provider: str, api_key: str) -> Dict[str, Any]:
        return await self._post("/v1/providers/test", {"provider": provider, "api_key": api_key})

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