"""
Auto-generated api_keys resource.

DO NOT EDIT THIS FILE MANUALLY.
Run `python scripts/generate_resources.py` to regenerate.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from ..client.base import BaseClient


class ApiKeysResource:
    """
    Manage API keys.
    """

    def __init__(self, client: BaseClient) -> None:
        self._client = client

    def _build_params(self, **kwargs: Any) -> dict[str, Any]:
        """Build query parameters, filtering None values."""

        def to_camel(s: str) -> str:
            parts = s.split("_")
            return parts[0] + "".join(p.title() for p in parts[1:])

        return {to_camel(k): v for k, v in kwargs.items() if v is not None}

    def _build_payload(self, **kwargs: Any) -> dict[str, Any]:
        """Build request payload, filtering None values."""
        from datetime import datetime

        def to_camel(s: str) -> str:
            parts = s.split("_")
            return parts[0] + "".join(p.title() for p in parts[1:])

        result: dict[str, Any] = {}
        for k, v in kwargs.items():
            if v is None:
                continue
            if isinstance(v, datetime):
                result[to_camel(k)] = v.isoformat()
            else:
                result[to_camel(k)] = v
        return result

    def list_api_keys(self) -> dict[str, Any]:
        """List keys"""
        return self._client._get("/v1/api-keys")

    def create_api_key(
        self,
        name: str,
        *,
        expires_in: int | None = None,
        scope: str | None = "full",
        profile_ids: list[str] | None = None,
        permission: str | None = "read-write",
    ) -> dict[str, Any]:
        """Create key"""
        payload = self._build_payload(
            name=name,
            expires_in=expires_in,
            scope=scope,
            profile_ids=profile_ids,
            permission=permission,
        )
        return self._client._post("/v1/api-keys", data=payload)

    def delete_api_key(self, key_id: str) -> dict[str, Any]:
        """Delete key"""
        return self._client._delete(f"/v1/api-keys/{key_id}")

    async def alist_api_keys(self) -> dict[str, Any]:
        """List keys (async)"""
        return await self._client._aget("/v1/api-keys")

    async def acreate_api_key(
        self,
        name: str,
        *,
        expires_in: int | None = None,
        scope: str | None = "full",
        profile_ids: list[str] | None = None,
        permission: str | None = "read-write",
    ) -> dict[str, Any]:
        """Create key (async)"""
        payload = self._build_payload(
            name=name,
            expires_in=expires_in,
            scope=scope,
            profile_ids=profile_ids,
            permission=permission,
        )
        return await self._client._apost("/v1/api-keys", data=payload)

    async def adelete_api_key(self, key_id: str) -> dict[str, Any]:
        """Delete key (async)"""
        return await self._client._adelete(f"/v1/api-keys/{key_id}")
