# 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.boarding_link_api_response import BoardingLinkApiResponse
from ..types.orgid import Orgid
from ..types.payabli_api_response_template_id import PayabliApiResponseTemplateId
from ..types.template_code import TemplateCode
from ..types.template_content import TemplateContent
from ..types.template_name import TemplateName
from ..types.template_query_record import TemplateQueryRecord
from ..types.template_query_response import TemplateQueryResponse
from .raw_client import AsyncRawTemplatesClient, RawTemplatesClient

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


class TemplatesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawTemplatesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawTemplatesClient
        """
        return self._raw_client

    def add_template(
        self,
        org_id_: int,
        *,
        org_id: typing.Optional[Orgid] = OMIT,
        pricing_id: typing.Optional[int] = OMIT,
        template_code: typing.Optional[TemplateCode] = OMIT,
        template_content: typing.Optional[TemplateContent] = OMIT,
        template_description: typing.Optional[str] = OMIT,
        template_name: typing.Optional[TemplateName] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PayabliApiResponseTemplateId:
        """
        Creates a boarding template in an organization.

        Parameters
        ----------
        org_id_ : int
            The numeric identifier for organization, assigned by Payabli.

        org_id : typing.Optional[Orgid]
            The ID of the organization the template belongs to.

        pricing_id : typing.Optional[int]

        template_code : typing.Optional[TemplateCode]

        template_content : typing.Optional[TemplateContent]

        template_description : typing.Optional[str]
            A description for the template.

        template_name : typing.Optional[TemplateName]

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

        Returns
        -------
        PayabliApiResponseTemplateId
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.templates.add_template(
            org_id_=123,
        )
        """
        _response = self._raw_client.add_template(
            org_id_,
            org_id=org_id,
            pricing_id=pricing_id,
            template_code=template_code,
            template_content=template_content,
            template_description=template_description,
            template_name=template_name,
            request_options=request_options,
        )
        return _response.data

    def delete_template(
        self, template_id: float, *, request_options: typing.Optional[RequestOptions] = None
    ) -> PayabliApiResponseTemplateId:
        """
        Deletes a template by ID.

        Parameters
        ----------
        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

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

        Returns
        -------
        PayabliApiResponseTemplateId
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.templates.delete_template(
            template_id=80.0,
        )
        """
        _response = self._raw_client.delete_template(template_id, request_options=request_options)
        return _response.data

    def getlink_template(
        self, ignore_empty: bool, template_id: float, *, request_options: typing.Optional[RequestOptions] = None
    ) -> BoardingLinkApiResponse:
        """
        Generates a boarding link from a boarding template.

        Parameters
        ----------
        ignore_empty : bool
            Ignore read-only and empty fields Default is `false`. If `ignoreEmpty` = `false` and any field is empty, then the request returns a failure response. If `ignoreEmpty` = `true`, the request returns the boarding link name regardless of whether fields are empty.

        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

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

        Returns
        -------
        BoardingLinkApiResponse
            Success.

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.templates.getlink_template(
            ignore_empty=True,
            template_id=80.0,
        )
        """
        _response = self._raw_client.getlink_template(ignore_empty, template_id, request_options=request_options)
        return _response.data

    def get_template(
        self, template_id: float, *, request_options: typing.Optional[RequestOptions] = None
    ) -> TemplateQueryRecord:
        """
        Retrieves a boarding template's details by ID.

        Parameters
        ----------
        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

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

        Returns
        -------
        TemplateQueryRecord
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.templates.get_template(
            template_id=80.0,
        )
        """
        _response = self._raw_client.get_template(template_id, request_options=request_options)
        return _response.data

    def list_templates(
        self,
        org_id: int,
        *,
        from_record: typing.Optional[int] = None,
        limit_record: typing.Optional[int] = None,
        parameters: typing.Optional[typing.Dict[str, typing.Optional[str]]] = None,
        sort_by: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TemplateQueryResponse:
        """
        Retrieves a list of boarding templates for an organization. Use filters to limit results. You can't make a request that includes filters from the API console in the documentation. The response won't be filtered. Instead, copy the request, remove `parameters=` and run the request in a different client.

        Parameters
        ----------
        org_id : int
            The numeric identifier for organization, assigned by Payabli.

        from_record : typing.Optional[int]
            The number of records to skip before starting to collect the result set.

        limit_record : typing.Optional[int]
            Max number of records to return for the query. Use `0` or negative value to return all records.

        parameters : typing.Optional[typing.Dict[str, typing.Optional[str]]]

            Collection of field names, conditions, and values used to filter the query.

            <Info>
              **You must remove `parameters=` from the request before you send it, otherwise Payabli will ignore the filters.**

              Because of a technical limitation, you can't make a request that includes filters from the API console on this page. The response won't be filtered. Instead, copy the request, remove `parameters=` and run the request in a different client.

              For example:

              --url https://api-sandbox.payabli.com/api/Query/transactions/org/236?parameters=totalAmount(gt)=1000&limitRecord=20

              should become:

              --url https://api-sandbox.payabli.com/api/Query/transactions/org/236?totalAmount(gt)=1000&limitRecord=20
            </Info>


            See [Filters and Conditions Reference](/developers/developer-guides/pay-ops-reporting-engine-overview#filters-and-conditions-reference) for help.

            List of field names accepted:
            - `createdAt` (gt, ge, lt, le, eq, ne)
            - `title` (ct, nct)
            - `description` (ct, nct)
            - `code` (ct, nct)
            - `orgParentname` (ct, nct)

            List of comparison accepted - enclosed between parentheses:
            - eq or empty => equal
            - gt => greater than
            - ge => greater or equal
            - lt => less than
            - le => less or equal
            - ne => not equal
            - ct => contains
            - nct => not contains
            - in => inside array
            - nin => not inside array

            List of parameters accepted:
            - limitRecord : max number of records for query (default="20", "0" or negative value for all)
            - fromRecord : initial record in query

            Example: title(ct)=hoa return all records with title containing "hoa"

        sort_by : typing.Optional[str]
            The field name to use for sorting results. Use `desc(field_name)` to sort descending by `field_name`, and use `asc(field_name)` to sort ascending by `field_name`.

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

        Returns
        -------
        TemplateQueryResponse
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.templates.list_templates(
            org_id=123,
            from_record=251,
            limit_record=0,
            sort_by="desc(field_name)",
        )
        """
        _response = self._raw_client.list_templates(
            org_id,
            from_record=from_record,
            limit_record=limit_record,
            parameters=parameters,
            sort_by=sort_by,
            request_options=request_options,
        )
        return _response.data

    def update_template(
        self,
        template_id: float,
        *,
        org_id: typing.Optional[Orgid] = OMIT,
        pricing_id: typing.Optional[int] = OMIT,
        template_code: typing.Optional[TemplateCode] = OMIT,
        template_content: typing.Optional[TemplateContent] = OMIT,
        template_description: typing.Optional[str] = OMIT,
        template_name: typing.Optional[TemplateName] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PayabliApiResponseTemplateId:
        """
        Updates a boarding template by ID.

        Parameters
        ----------
        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

        org_id : typing.Optional[Orgid]
            The ID of the organization the template belongs to.

        pricing_id : typing.Optional[int]

        template_code : typing.Optional[TemplateCode]

        template_content : typing.Optional[TemplateContent]

        template_description : typing.Optional[str]
            A description for the template.

        template_name : typing.Optional[TemplateName]

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

        Returns
        -------
        PayabliApiResponseTemplateId
            Success

        Examples
        --------
        from payabli import payabli

        client = payabli(
            api_key="YOUR_API_KEY",
        )
        client.templates.update_template(
            template_id=80.0,
        )
        """
        _response = self._raw_client.update_template(
            template_id,
            org_id=org_id,
            pricing_id=pricing_id,
            template_code=template_code,
            template_content=template_content,
            template_description=template_description,
            template_name=template_name,
            request_options=request_options,
        )
        return _response.data


class AsyncTemplatesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawTemplatesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawTemplatesClient
        """
        return self._raw_client

    async def add_template(
        self,
        org_id_: int,
        *,
        org_id: typing.Optional[Orgid] = OMIT,
        pricing_id: typing.Optional[int] = OMIT,
        template_code: typing.Optional[TemplateCode] = OMIT,
        template_content: typing.Optional[TemplateContent] = OMIT,
        template_description: typing.Optional[str] = OMIT,
        template_name: typing.Optional[TemplateName] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PayabliApiResponseTemplateId:
        """
        Creates a boarding template in an organization.

        Parameters
        ----------
        org_id_ : int
            The numeric identifier for organization, assigned by Payabli.

        org_id : typing.Optional[Orgid]
            The ID of the organization the template belongs to.

        pricing_id : typing.Optional[int]

        template_code : typing.Optional[TemplateCode]

        template_content : typing.Optional[TemplateContent]

        template_description : typing.Optional[str]
            A description for the template.

        template_name : typing.Optional[TemplateName]

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

        Returns
        -------
        PayabliApiResponseTemplateId
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.templates.add_template(
                org_id_=123,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.add_template(
            org_id_,
            org_id=org_id,
            pricing_id=pricing_id,
            template_code=template_code,
            template_content=template_content,
            template_description=template_description,
            template_name=template_name,
            request_options=request_options,
        )
        return _response.data

    async def delete_template(
        self, template_id: float, *, request_options: typing.Optional[RequestOptions] = None
    ) -> PayabliApiResponseTemplateId:
        """
        Deletes a template by ID.

        Parameters
        ----------
        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

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

        Returns
        -------
        PayabliApiResponseTemplateId
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.templates.delete_template(
                template_id=80.0,
            )


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

    async def getlink_template(
        self, ignore_empty: bool, template_id: float, *, request_options: typing.Optional[RequestOptions] = None
    ) -> BoardingLinkApiResponse:
        """
        Generates a boarding link from a boarding template.

        Parameters
        ----------
        ignore_empty : bool
            Ignore read-only and empty fields Default is `false`. If `ignoreEmpty` = `false` and any field is empty, then the request returns a failure response. If `ignoreEmpty` = `true`, the request returns the boarding link name regardless of whether fields are empty.

        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

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

        Returns
        -------
        BoardingLinkApiResponse
            Success.

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.templates.getlink_template(
                ignore_empty=True,
                template_id=80.0,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.getlink_template(ignore_empty, template_id, request_options=request_options)
        return _response.data

    async def get_template(
        self, template_id: float, *, request_options: typing.Optional[RequestOptions] = None
    ) -> TemplateQueryRecord:
        """
        Retrieves a boarding template's details by ID.

        Parameters
        ----------
        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

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

        Returns
        -------
        TemplateQueryRecord
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.templates.get_template(
                template_id=80.0,
            )


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

    async def list_templates(
        self,
        org_id: int,
        *,
        from_record: typing.Optional[int] = None,
        limit_record: typing.Optional[int] = None,
        parameters: typing.Optional[typing.Dict[str, typing.Optional[str]]] = None,
        sort_by: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> TemplateQueryResponse:
        """
        Retrieves a list of boarding templates for an organization. Use filters to limit results. You can't make a request that includes filters from the API console in the documentation. The response won't be filtered. Instead, copy the request, remove `parameters=` and run the request in a different client.

        Parameters
        ----------
        org_id : int
            The numeric identifier for organization, assigned by Payabli.

        from_record : typing.Optional[int]
            The number of records to skip before starting to collect the result set.

        limit_record : typing.Optional[int]
            Max number of records to return for the query. Use `0` or negative value to return all records.

        parameters : typing.Optional[typing.Dict[str, typing.Optional[str]]]

            Collection of field names, conditions, and values used to filter the query.

            <Info>
              **You must remove `parameters=` from the request before you send it, otherwise Payabli will ignore the filters.**

              Because of a technical limitation, you can't make a request that includes filters from the API console on this page. The response won't be filtered. Instead, copy the request, remove `parameters=` and run the request in a different client.

              For example:

              --url https://api-sandbox.payabli.com/api/Query/transactions/org/236?parameters=totalAmount(gt)=1000&limitRecord=20

              should become:

              --url https://api-sandbox.payabli.com/api/Query/transactions/org/236?totalAmount(gt)=1000&limitRecord=20
            </Info>


            See [Filters and Conditions Reference](/developers/developer-guides/pay-ops-reporting-engine-overview#filters-and-conditions-reference) for help.

            List of field names accepted:
            - `createdAt` (gt, ge, lt, le, eq, ne)
            - `title` (ct, nct)
            - `description` (ct, nct)
            - `code` (ct, nct)
            - `orgParentname` (ct, nct)

            List of comparison accepted - enclosed between parentheses:
            - eq or empty => equal
            - gt => greater than
            - ge => greater or equal
            - lt => less than
            - le => less or equal
            - ne => not equal
            - ct => contains
            - nct => not contains
            - in => inside array
            - nin => not inside array

            List of parameters accepted:
            - limitRecord : max number of records for query (default="20", "0" or negative value for all)
            - fromRecord : initial record in query

            Example: title(ct)=hoa return all records with title containing "hoa"

        sort_by : typing.Optional[str]
            The field name to use for sorting results. Use `desc(field_name)` to sort descending by `field_name`, and use `asc(field_name)` to sort ascending by `field_name`.

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

        Returns
        -------
        TemplateQueryResponse
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.templates.list_templates(
                org_id=123,
                from_record=251,
                limit_record=0,
                sort_by="desc(field_name)",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list_templates(
            org_id,
            from_record=from_record,
            limit_record=limit_record,
            parameters=parameters,
            sort_by=sort_by,
            request_options=request_options,
        )
        return _response.data

    async def update_template(
        self,
        template_id: float,
        *,
        org_id: typing.Optional[Orgid] = OMIT,
        pricing_id: typing.Optional[int] = OMIT,
        template_code: typing.Optional[TemplateCode] = OMIT,
        template_content: typing.Optional[TemplateContent] = OMIT,
        template_description: typing.Optional[str] = OMIT,
        template_name: typing.Optional[TemplateName] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PayabliApiResponseTemplateId:
        """
        Updates a boarding template by ID.

        Parameters
        ----------
        template_id : float
            The boarding template ID. Can be found at the end of the boarding template URL in PartnerHub. Example: `https://partner-sandbox.payabli.com/myorganization/boarding/edittemplate/80`. Here, the template ID is `80`.

        org_id : typing.Optional[Orgid]
            The ID of the organization the template belongs to.

        pricing_id : typing.Optional[int]

        template_code : typing.Optional[TemplateCode]

        template_content : typing.Optional[TemplateContent]

        template_description : typing.Optional[str]
            A description for the template.

        template_name : typing.Optional[TemplateName]

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

        Returns
        -------
        PayabliApiResponseTemplateId
            Success

        Examples
        --------
        import asyncio

        from payabli import Asyncpayabli

        client = Asyncpayabli(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.templates.update_template(
                template_id=80.0,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update_template(
            template_id,
            org_id=org_id,
            pricing_id=pricing_id,
            template_code=template_code,
            template_content=template_content,
            template_description=template_description,
            template_name=template_name,
            request_options=request_options,
        )
        return _response.data
