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

import datetime as dt
import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.service_account_api_key_list_response import ServiceAccountApiKeyListResponse
from ..types.service_account_api_key_response import ServiceAccountApiKeyResponse
from ..types.service_account_list_response import ServiceAccountListResponse
from ..types.service_account_response import ServiceAccountResponse
from .raw_client import AsyncRawServiceAccountsClient, RawServiceAccountsClient
from .types.service_account_create_account_type import ServiceAccountCreateAccountType

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


class ServiceAccountsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawServiceAccountsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawServiceAccountsClient
        """
        return self._raw_client

    def list_service_accounts(
        self,
        organization_id: str,
        *,
        page: typing.Optional[int] = None,
        page_size: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountListResponse:
        """
        List all service accounts in an organization. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        page : typing.Optional[int]
            Page number

        page_size : typing.Optional[int]
            Items per page

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

        Returns
        -------
        ServiceAccountListResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.list_service_accounts(
            organization_id="organization_id",
        )
        """
        _response = self._raw_client.list_service_accounts(
            organization_id, page=page, page_size=page_size, request_options=request_options
        )
        return _response.data

    def create_service_account(
        self,
        organization_id: str,
        *,
        name: str,
        account_type: ServiceAccountCreateAccountType,
        description: typing.Optional[str] = OMIT,
        assistant_id: typing.Optional[str] = OMIT,
        capabilities: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountResponse:
        """
        Create a new service account for programmatic API access. Supports 'assistant' type (linked to an assistant) and 'bot' type (standalone). Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        name : str
            Display name for the service account

        account_type : ServiceAccountCreateAccountType
            Account type: 'assistant' or 'bot'

        description : typing.Optional[str]
            Description of what this account does

        assistant_id : typing.Optional[str]
            Assistant ID (required if account_type is 'assistant')

        capabilities : typing.Optional[typing.Sequence[str]]
            List of capability strings to assign to the service account

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

        Returns
        -------
        ServiceAccountResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.create_service_account(
            organization_id="organization_id",
            name="name",
            account_type="assistant",
        )
        """
        _response = self._raw_client.create_service_account(
            organization_id,
            name=name,
            account_type=account_type,
            description=description,
            assistant_id=assistant_id,
            capabilities=capabilities,
            request_options=request_options,
        )
        return _response.data

    def get_service_account(
        self, organization_id: str, service_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ServiceAccountResponse:
        """
        Get details of a specific service account. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

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

        Returns
        -------
        ServiceAccountResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.get_service_account(
            organization_id="organization_id",
            service_account_id="service_account_id",
        )
        """
        _response = self._raw_client.get_service_account(
            organization_id, service_account_id, request_options=request_options
        )
        return _response.data

    def update_service_account(
        self,
        organization_id: str,
        service_account_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        capabilities: typing.Optional[typing.Sequence[str]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountResponse:
        """
        Update name, description, capabilities, or active status. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

        name : typing.Optional[str]
            Display name

        description : typing.Optional[str]
            Description

        capabilities : typing.Optional[typing.Sequence[str]]
            List of capability strings to assign to the service account

        is_active : typing.Optional[bool]
            Enable or disable the account

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

        Returns
        -------
        ServiceAccountResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.update_service_account(
            organization_id="organization_id",
            service_account_id="service_account_id",
        )
        """
        _response = self._raw_client.update_service_account(
            organization_id,
            service_account_id,
            name=name,
            description=description,
            capabilities=capabilities,
            is_active=is_active,
            request_options=request_options,
        )
        return _response.data

    def delete_service_account(
        self, organization_id: str, service_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Any:
        """
        Soft-delete a service account and revoke all its API keys. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

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

        Returns
        -------
        typing.Any
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.delete_service_account(
            organization_id="organization_id",
            service_account_id="service_account_id",
        )
        """
        _response = self._raw_client.delete_service_account(
            organization_id, service_account_id, request_options=request_options
        )
        return _response.data

    def list_service_account_api_keys(
        self, organization_id: str, service_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ServiceAccountApiKeyListResponse:
        """
        List all API keys belonging to a service account. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

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

        Returns
        -------
        ServiceAccountApiKeyListResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.list_service_account_api_keys(
            organization_id="organization_id",
            service_account_id="service_account_id",
        )
        """
        _response = self._raw_client.list_service_account_api_keys(
            organization_id, service_account_id, request_options=request_options
        )
        return _response.data

    def create_service_account_api_key(
        self,
        organization_id: str,
        service_account_id: str,
        *,
        key_name: str,
        expires_at: typing.Optional[dt.datetime] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountApiKeyResponse:
        """
        Generate a new API key for a service account. The raw key is returned once and must be stored securely. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

        key_name : str
            Name for the API key

        expires_at : typing.Optional[dt.datetime]
            Optional expiration

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

        Returns
        -------
        ServiceAccountApiKeyResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.create_service_account_api_key(
            organization_id="organization_id",
            service_account_id="service_account_id",
            key_name="key_name",
        )
        """
        _response = self._raw_client.create_service_account_api_key(
            organization_id,
            service_account_id,
            key_name=key_name,
            expires_at=expires_at,
            request_options=request_options,
        )
        return _response.data

    def delete_service_account_api_key(
        self,
        organization_id: str,
        service_account_id: str,
        key_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Dict[str, typing.Any]:
        """
        Revoke (soft-delete) an API key belonging to a service account. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

        key_id : str
            API key ID

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

        Returns
        -------
        typing.Dict[str, typing.Any]
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.service_accounts.delete_service_account_api_key(
            organization_id="organization_id",
            service_account_id="service_account_id",
            key_id="key_id",
        )
        """
        _response = self._raw_client.delete_service_account_api_key(
            organization_id, service_account_id, key_id, request_options=request_options
        )
        return _response.data


class AsyncServiceAccountsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawServiceAccountsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawServiceAccountsClient
        """
        return self._raw_client

    async def list_service_accounts(
        self,
        organization_id: str,
        *,
        page: typing.Optional[int] = None,
        page_size: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountListResponse:
        """
        List all service accounts in an organization. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        page : typing.Optional[int]
            Page number

        page_size : typing.Optional[int]
            Items per page

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

        Returns
        -------
        ServiceAccountListResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.list_service_accounts(
                organization_id="organization_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list_service_accounts(
            organization_id, page=page, page_size=page_size, request_options=request_options
        )
        return _response.data

    async def create_service_account(
        self,
        organization_id: str,
        *,
        name: str,
        account_type: ServiceAccountCreateAccountType,
        description: typing.Optional[str] = OMIT,
        assistant_id: typing.Optional[str] = OMIT,
        capabilities: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountResponse:
        """
        Create a new service account for programmatic API access. Supports 'assistant' type (linked to an assistant) and 'bot' type (standalone). Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        name : str
            Display name for the service account

        account_type : ServiceAccountCreateAccountType
            Account type: 'assistant' or 'bot'

        description : typing.Optional[str]
            Description of what this account does

        assistant_id : typing.Optional[str]
            Assistant ID (required if account_type is 'assistant')

        capabilities : typing.Optional[typing.Sequence[str]]
            List of capability strings to assign to the service account

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

        Returns
        -------
        ServiceAccountResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.create_service_account(
                organization_id="organization_id",
                name="name",
                account_type="assistant",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_service_account(
            organization_id,
            name=name,
            account_type=account_type,
            description=description,
            assistant_id=assistant_id,
            capabilities=capabilities,
            request_options=request_options,
        )
        return _response.data

    async def get_service_account(
        self, organization_id: str, service_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ServiceAccountResponse:
        """
        Get details of a specific service account. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

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

        Returns
        -------
        ServiceAccountResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.get_service_account(
                organization_id="organization_id",
                service_account_id="service_account_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.get_service_account(
            organization_id, service_account_id, request_options=request_options
        )
        return _response.data

    async def update_service_account(
        self,
        organization_id: str,
        service_account_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        capabilities: typing.Optional[typing.Sequence[str]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountResponse:
        """
        Update name, description, capabilities, or active status. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

        name : typing.Optional[str]
            Display name

        description : typing.Optional[str]
            Description

        capabilities : typing.Optional[typing.Sequence[str]]
            List of capability strings to assign to the service account

        is_active : typing.Optional[bool]
            Enable or disable the account

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

        Returns
        -------
        ServiceAccountResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.update_service_account(
                organization_id="organization_id",
                service_account_id="service_account_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update_service_account(
            organization_id,
            service_account_id,
            name=name,
            description=description,
            capabilities=capabilities,
            is_active=is_active,
            request_options=request_options,
        )
        return _response.data

    async def delete_service_account(
        self, organization_id: str, service_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Any:
        """
        Soft-delete a service account and revoke all its API keys. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

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

        Returns
        -------
        typing.Any
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.delete_service_account(
                organization_id="organization_id",
                service_account_id="service_account_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete_service_account(
            organization_id, service_account_id, request_options=request_options
        )
        return _response.data

    async def list_service_account_api_keys(
        self, organization_id: str, service_account_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> ServiceAccountApiKeyListResponse:
        """
        List all API keys belonging to a service account. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

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

        Returns
        -------
        ServiceAccountApiKeyListResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.list_service_account_api_keys(
                organization_id="organization_id",
                service_account_id="service_account_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list_service_account_api_keys(
            organization_id, service_account_id, request_options=request_options
        )
        return _response.data

    async def create_service_account_api_key(
        self,
        organization_id: str,
        service_account_id: str,
        *,
        key_name: str,
        expires_at: typing.Optional[dt.datetime] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ServiceAccountApiKeyResponse:
        """
        Generate a new API key for a service account. The raw key is returned once and must be stored securely. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

        key_name : str
            Name for the API key

        expires_at : typing.Optional[dt.datetime]
            Optional expiration

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

        Returns
        -------
        ServiceAccountApiKeyResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.create_service_account_api_key(
                organization_id="organization_id",
                service_account_id="service_account_id",
                key_name="key_name",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_service_account_api_key(
            organization_id,
            service_account_id,
            key_name=key_name,
            expires_at=expires_at,
            request_options=request_options,
        )
        return _response.data

    async def delete_service_account_api_key(
        self,
        organization_id: str,
        service_account_id: str,
        key_id: str,
        *,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Dict[str, typing.Any]:
        """
        Revoke (soft-delete) an API key belonging to a service account. Requires manage_service_accounts capability.

        Parameters
        ----------
        organization_id : str

        service_account_id : str
            Service account ID

        key_id : str
            API key ID

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

        Returns
        -------
        typing.Dict[str, typing.Any]
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.service_accounts.delete_service_account_api_key(
                organization_id="organization_id",
                service_account_id="service_account_id",
                key_id="key_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete_service_account_api_key(
            organization_id, service_account_id, key_id, request_options=request_options
        )
        return _response.data
