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

from __future__ import annotations

import typing

from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.pagination import AsyncPager, SyncPager
from ...core.request_options import RequestOptions
from ...types.action_module_action import ActionModuleAction
from ...types.action_module_dependency_request import ActionModuleDependencyRequest
from ...types.action_module_list_item import ActionModuleListItem
from ...types.action_module_secret_request import ActionModuleSecretRequest
from ...types.create_action_module_response_content import CreateActionModuleResponseContent
from ...types.get_action_module_actions_response_content import GetActionModuleActionsResponseContent
from ...types.get_action_module_response_content import GetActionModuleResponseContent
from ...types.get_action_modules_response_content import GetActionModulesResponseContent
from ...types.rollback_action_module_response_content import RollbackActionModuleResponseContent
from ...types.update_action_module_response_content import UpdateActionModuleResponseContent
from .raw_client import AsyncRawModulesClient, RawModulesClient

if typing.TYPE_CHECKING:
    from .versions.client import AsyncVersionsClient, VersionsClient
# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class ModulesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawModulesClient(client_wrapper=client_wrapper)
        self._client_wrapper = client_wrapper
        self._versions: typing.Optional[VersionsClient] = None

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

        Returns
        -------
        RawModulesClient
        """
        return self._raw_client

    def list(
        self,
        *,
        page: typing.Optional[int] = 0,
        per_page: typing.Optional[int] = 50,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[ActionModuleListItem, GetActionModulesResponseContent]:
        """
        Retrieve a paginated list of all Actions Modules with optional filtering and totals.

        Parameters
        ----------
        page : typing.Optional[int]
            Page index of the results to return. First page is 0.

        per_page : typing.Optional[int]
            Number of results per page. Paging is disabled if parameter not sent.

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

        Returns
        -------
        SyncPager[ActionModuleListItem, GetActionModulesResponseContent]
            The action modules were retrieved.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        response = client.actions.modules.list(
            page=1,
            per_page=1,
        )
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        return self._raw_client.list(page=page, per_page=per_page, request_options=request_options)

    def create(
        self,
        *,
        name: str,
        code: str,
        secrets: typing.Optional[typing.Sequence[ActionModuleSecretRequest]] = OMIT,
        dependencies: typing.Optional[typing.Sequence[ActionModuleDependencyRequest]] = OMIT,
        api_version: typing.Optional[str] = OMIT,
        publish: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateActionModuleResponseContent:
        """
        Create a new Actions Module for reusable code across actions.

        Parameters
        ----------
        name : str
            The name of the action module.

        code : str
            The source code of the action module.

        secrets : typing.Optional[typing.Sequence[ActionModuleSecretRequest]]
            The secrets to associate with the action module.

        dependencies : typing.Optional[typing.Sequence[ActionModuleDependencyRequest]]
            The npm dependencies of the action module.

        api_version : typing.Optional[str]
            The API version of the module.

        publish : typing.Optional[bool]
            Whether to publish the module immediately after creation.

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

        Returns
        -------
        CreateActionModuleResponseContent
            The action module was created.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        client.actions.modules.create(
            name="name",
            code="code",
        )
        """
        _response = self._raw_client.create(
            name=name,
            code=code,
            secrets=secrets,
            dependencies=dependencies,
            api_version=api_version,
            publish=publish,
            request_options=request_options,
        )
        return _response.data

    def get(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetActionModuleResponseContent:
        """
        Retrieve details of a specific Actions Module by its unique identifier.

        Parameters
        ----------
        id : str
            The ID of the action module to retrieve.

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

        Returns
        -------
        GetActionModuleResponseContent
            The action module was retrieved.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        client.actions.modules.get(
            id="id",
        )
        """
        _response = self._raw_client.get(id, request_options=request_options)
        return _response.data

    def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Permanently delete an Actions Module. This will fail if the module is still in use by any actions.

        Parameters
        ----------
        id : str
            The ID of the Actions Module to delete.

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

        Returns
        -------
        None

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        client.actions.modules.delete(
            id="id",
        )
        """
        _response = self._raw_client.delete(id, request_options=request_options)
        return _response.data

    def update(
        self,
        id: str,
        *,
        code: typing.Optional[str] = OMIT,
        secrets: typing.Optional[typing.Sequence[ActionModuleSecretRequest]] = OMIT,
        dependencies: typing.Optional[typing.Sequence[ActionModuleDependencyRequest]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> UpdateActionModuleResponseContent:
        """
        Update properties of an existing Actions Module, such as code, dependencies, or secrets.

        Parameters
        ----------
        id : str
            The ID of the action module to update.

        code : typing.Optional[str]
            The source code of the action module.

        secrets : typing.Optional[typing.Sequence[ActionModuleSecretRequest]]
            The secrets to associate with the action module.

        dependencies : typing.Optional[typing.Sequence[ActionModuleDependencyRequest]]
            The npm dependencies of the action module.

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

        Returns
        -------
        UpdateActionModuleResponseContent
            The action module was updated.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        client.actions.modules.update(
            id="id",
        )
        """
        _response = self._raw_client.update(
            id, code=code, secrets=secrets, dependencies=dependencies, request_options=request_options
        )
        return _response.data

    def list_actions(
        self,
        id: str,
        *,
        page: typing.Optional[int] = 0,
        per_page: typing.Optional[int] = 50,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[ActionModuleAction, GetActionModuleActionsResponseContent]:
        """
        Lists all actions that are using a specific Actions Module, showing which deployed action versions reference this Actions Module.

        Parameters
        ----------
        id : str
            The unique ID of the module.

        page : typing.Optional[int]
            Page index of the results to return. First page is 0.

        per_page : typing.Optional[int]
            Number of results per page.

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

        Returns
        -------
        SyncPager[ActionModuleAction, GetActionModuleActionsResponseContent]
            The actions were retrieved.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        response = client.actions.modules.list_actions(
            id="id",
            page=1,
            per_page=1,
        )
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        return self._raw_client.list_actions(id, page=page, per_page=per_page, request_options=request_options)

    def rollback(
        self, id: str, *, module_version_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> RollbackActionModuleResponseContent:
        """
        Rolls back an Actions Module's draft to a previously created version. This action copies the code, dependencies, and secrets from the specified version into the current draft.

        Parameters
        ----------
        id : str
            The unique ID of the module to roll back.

        module_version_id : str
            The unique ID of the module version to roll back to.

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

        Returns
        -------
        RollbackActionModuleResponseContent
            The rollback was successful.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        client.actions.modules.rollback(
            id="id",
            module_version_id="module_version_id",
        )
        """
        _response = self._raw_client.rollback(id, module_version_id=module_version_id, request_options=request_options)
        return _response.data

    @property
    def versions(self):
        if self._versions is None:
            from .versions.client import VersionsClient  # noqa: E402

            self._versions = VersionsClient(client_wrapper=self._client_wrapper)
        return self._versions


class AsyncModulesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawModulesClient(client_wrapper=client_wrapper)
        self._client_wrapper = client_wrapper
        self._versions: typing.Optional[AsyncVersionsClient] = None

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

        Returns
        -------
        AsyncRawModulesClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        page: typing.Optional[int] = 0,
        per_page: typing.Optional[int] = 50,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[ActionModuleListItem, GetActionModulesResponseContent]:
        """
        Retrieve a paginated list of all Actions Modules with optional filtering and totals.

        Parameters
        ----------
        page : typing.Optional[int]
            Page index of the results to return. First page is 0.

        per_page : typing.Optional[int]
            Number of results per page. Paging is disabled if parameter not sent.

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

        Returns
        -------
        AsyncPager[ActionModuleListItem, GetActionModulesResponseContent]
            The action modules were retrieved.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            response = await client.actions.modules.list(
                page=1,
                per_page=1,
            )
            async for item in response:
                yield item

            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page


        asyncio.run(main())
        """
        return await self._raw_client.list(page=page, per_page=per_page, request_options=request_options)

    async def create(
        self,
        *,
        name: str,
        code: str,
        secrets: typing.Optional[typing.Sequence[ActionModuleSecretRequest]] = OMIT,
        dependencies: typing.Optional[typing.Sequence[ActionModuleDependencyRequest]] = OMIT,
        api_version: typing.Optional[str] = OMIT,
        publish: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> CreateActionModuleResponseContent:
        """
        Create a new Actions Module for reusable code across actions.

        Parameters
        ----------
        name : str
            The name of the action module.

        code : str
            The source code of the action module.

        secrets : typing.Optional[typing.Sequence[ActionModuleSecretRequest]]
            The secrets to associate with the action module.

        dependencies : typing.Optional[typing.Sequence[ActionModuleDependencyRequest]]
            The npm dependencies of the action module.

        api_version : typing.Optional[str]
            The API version of the module.

        publish : typing.Optional[bool]
            Whether to publish the module immediately after creation.

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

        Returns
        -------
        CreateActionModuleResponseContent
            The action module was created.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.actions.modules.create(
                name="name",
                code="code",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            name=name,
            code=code,
            secrets=secrets,
            dependencies=dependencies,
            api_version=api_version,
            publish=publish,
            request_options=request_options,
        )
        return _response.data

    async def get(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetActionModuleResponseContent:
        """
        Retrieve details of a specific Actions Module by its unique identifier.

        Parameters
        ----------
        id : str
            The ID of the action module to retrieve.

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

        Returns
        -------
        GetActionModuleResponseContent
            The action module was retrieved.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.actions.modules.get(
                id="id",
            )


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

    async def delete(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Permanently delete an Actions Module. This will fail if the module is still in use by any actions.

        Parameters
        ----------
        id : str
            The ID of the Actions Module to delete.

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.actions.modules.delete(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete(id, request_options=request_options)
        return _response.data

    async def update(
        self,
        id: str,
        *,
        code: typing.Optional[str] = OMIT,
        secrets: typing.Optional[typing.Sequence[ActionModuleSecretRequest]] = OMIT,
        dependencies: typing.Optional[typing.Sequence[ActionModuleDependencyRequest]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> UpdateActionModuleResponseContent:
        """
        Update properties of an existing Actions Module, such as code, dependencies, or secrets.

        Parameters
        ----------
        id : str
            The ID of the action module to update.

        code : typing.Optional[str]
            The source code of the action module.

        secrets : typing.Optional[typing.Sequence[ActionModuleSecretRequest]]
            The secrets to associate with the action module.

        dependencies : typing.Optional[typing.Sequence[ActionModuleDependencyRequest]]
            The npm dependencies of the action module.

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

        Returns
        -------
        UpdateActionModuleResponseContent
            The action module was updated.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.actions.modules.update(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update(
            id, code=code, secrets=secrets, dependencies=dependencies, request_options=request_options
        )
        return _response.data

    async def list_actions(
        self,
        id: str,
        *,
        page: typing.Optional[int] = 0,
        per_page: typing.Optional[int] = 50,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[ActionModuleAction, GetActionModuleActionsResponseContent]:
        """
        Lists all actions that are using a specific Actions Module, showing which deployed action versions reference this Actions Module.

        Parameters
        ----------
        id : str
            The unique ID of the module.

        page : typing.Optional[int]
            Page index of the results to return. First page is 0.

        per_page : typing.Optional[int]
            Number of results per page.

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

        Returns
        -------
        AsyncPager[ActionModuleAction, GetActionModuleActionsResponseContent]
            The actions were retrieved.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            response = await client.actions.modules.list_actions(
                id="id",
                page=1,
                per_page=1,
            )
            async for item in response:
                yield item

            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page


        asyncio.run(main())
        """
        return await self._raw_client.list_actions(id, page=page, per_page=per_page, request_options=request_options)

    async def rollback(
        self, id: str, *, module_version_id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> RollbackActionModuleResponseContent:
        """
        Rolls back an Actions Module's draft to a previously created version. This action copies the code, dependencies, and secrets from the specified version into the current draft.

        Parameters
        ----------
        id : str
            The unique ID of the module to roll back.

        module_version_id : str
            The unique ID of the module version to roll back to.

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

        Returns
        -------
        RollbackActionModuleResponseContent
            The rollback was successful.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.actions.modules.rollback(
                id="id",
                module_version_id="module_version_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.rollback(
            id, module_version_id=module_version_id, request_options=request_options
        )
        return _response.data

    @property
    def versions(self):
        if self._versions is None:
            from .versions.client import AsyncVersionsClient  # noqa: E402

            self._versions = AsyncVersionsClient(client_wrapper=self._client_wrapper)
        return self._versions
