from __future__ import annotations

"""API key management resource."""

from nexure.models import ApiKeyCreated, ApiKeyList
from nexure.resources._base import BaseResource


class ApiKeysResource(BaseResource):
    """nx.api_keys — manage API keys."""

    def create(self, *, name: str | None = None) -> ApiKeyCreated:
        """Create a new API key. The raw key is returned only once.

        Args:
            name: Display name for the key.
        """
        body = {}
        if name is not None:
            body["name"] = name
        data = self._post("/api-keys", json=body)
        return ApiKeyCreated(**data)

    def list(self) -> ApiKeyList:
        """List all API keys for the tenant (masked)."""
        data = self._get("/api-keys")
        return ApiKeyList(**data)

    def revoke(self, key_id: str) -> None:
        """Revoke an API key.

        Args:
            key_id: The ID of the key to revoke.
        """
        self._delete(f"/api-keys/{key_id}")
