# This file was auto-generated by Fern from our API Definition.

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.token_response import TokenResponse
from .raw_client import AsyncRawTokensClient, RawTokensClient

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class TokensClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawTokensClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawTokensClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawTokensClient
        """
        return self._raw_client

    def issue(
        self,
        *,
        name: str,
        user_id: str,
        admin_key: typing.Optional[str] = None,
        scopes: typing.Optional[typing.Sequence[str]] = OMIT,
        expires_in_days: typing.Optional[int] = OMIT,
        revoke_existing: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Optional[typing.Any]:
        """
        Create a personal access token (admin only).

        Parameters
        ----------
        name : str
            Token name

        user_id : str
            User ID

        admin_key : typing.Optional[str]

        scopes : typing.Optional[typing.Sequence[str]]
            Token scopes

        expires_in_days : typing.Optional[int]
            Token expiration in days

        revoke_existing : typing.Optional[bool]
            Revoke existing tokens for user

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.Optional[typing.Any]
            Successful Response

        Examples
        --------
        from ai_infra import Sandbox

        client = Sandbox(
            token="YOUR_TOKEN",
        )
        client.tokens.issue(
            name="name",
            user_id="user_id",
        )
        """
        _response = self._raw_client.issue(
            name=name,
            user_id=user_id,
            admin_key=admin_key,
            scopes=scopes,
            expires_in_days=expires_in_days,
            revoke_existing=revoke_existing,
            request_options=request_options,
        )
        return _response.data

    def revoke(
        self,
        prefix: str,
        *,
        admin_key: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Optional[typing.Any]:
        """
        Revoke a personal access token (admin only).

        Parameters
        ----------
        prefix : str

        admin_key : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.Optional[typing.Any]
            Successful Response

        Examples
        --------
        from ai_infra import Sandbox

        client = Sandbox(
            token="YOUR_TOKEN",
        )
        client.tokens.revoke(
            prefix="prefix",
        )
        """
        _response = self._raw_client.revoke(prefix, admin_key=admin_key, request_options=request_options)
        return _response.data

    def get(
        self,
        user_id: str,
        *,
        include_raw_token: typing.Optional[bool] = None,
        admin_key: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TokenResponse:
        """
        Get the most recent token for a user (admin only).

        Parameters
        ----------
        user_id : str

        include_raw_token : typing.Optional[bool]

        admin_key : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        TokenResponse
            Successful Response

        Examples
        --------
        from ai_infra import Sandbox

        client = Sandbox(
            token="YOUR_TOKEN",
        )
        client.tokens.get(
            user_id="user_id",
        )
        """
        _response = self._raw_client.get(
            user_id, include_raw_token=include_raw_token, admin_key=admin_key, request_options=request_options
        )
        return _response.data


class AsyncTokensClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawTokensClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawTokensClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawTokensClient
        """
        return self._raw_client

    async def issue(
        self,
        *,
        name: str,
        user_id: str,
        admin_key: typing.Optional[str] = None,
        scopes: typing.Optional[typing.Sequence[str]] = OMIT,
        expires_in_days: typing.Optional[int] = OMIT,
        revoke_existing: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Optional[typing.Any]:
        """
        Create a personal access token (admin only).

        Parameters
        ----------
        name : str
            Token name

        user_id : str
            User ID

        admin_key : typing.Optional[str]

        scopes : typing.Optional[typing.Sequence[str]]
            Token scopes

        expires_in_days : typing.Optional[int]
            Token expiration in days

        revoke_existing : typing.Optional[bool]
            Revoke existing tokens for user

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.Optional[typing.Any]
            Successful Response

        Examples
        --------
        import asyncio

        from ai_infra import AsyncSandbox

        client = AsyncSandbox(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.tokens.issue(
                name="name",
                user_id="user_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.issue(
            name=name,
            user_id=user_id,
            admin_key=admin_key,
            scopes=scopes,
            expires_in_days=expires_in_days,
            revoke_existing=revoke_existing,
            request_options=request_options,
        )
        return _response.data

    async def revoke(
        self,
        prefix: str,
        *,
        admin_key: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Optional[typing.Any]:
        """
        Revoke a personal access token (admin only).

        Parameters
        ----------
        prefix : str

        admin_key : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.Optional[typing.Any]
            Successful Response

        Examples
        --------
        import asyncio

        from ai_infra import AsyncSandbox

        client = AsyncSandbox(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.tokens.revoke(
                prefix="prefix",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.revoke(prefix, admin_key=admin_key, request_options=request_options)
        return _response.data

    async def get(
        self,
        user_id: str,
        *,
        include_raw_token: typing.Optional[bool] = None,
        admin_key: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TokenResponse:
        """
        Get the most recent token for a user (admin only).

        Parameters
        ----------
        user_id : str

        include_raw_token : typing.Optional[bool]

        admin_key : typing.Optional[str]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        TokenResponse
            Successful Response

        Examples
        --------
        import asyncio

        from ai_infra import AsyncSandbox

        client = AsyncSandbox(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.tokens.get(
                user_id="user_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get(
            user_id, include_raw_token=include_raw_token, admin_key=admin_key, request_options=request_options
        )
        return _response.data
