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

import typing
from ..core.client_wrapper import SyncClientWrapper
from .raw_client import RawLoyaltyClient
from .accounts.client import AccountsClient
from .programs.client import ProgramsClient
from .rewards.client import RewardsClient
from ..requests.loyalty_event_query import LoyaltyEventQueryParams
from ..core.request_options import RequestOptions
from ..types.search_loyalty_events_response import SearchLoyaltyEventsResponse
from ..core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawLoyaltyClient
from .accounts.client import AsyncAccountsClient
from .programs.client import AsyncProgramsClient
from .rewards.client import AsyncRewardsClient

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


class LoyaltyClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawLoyaltyClient(client_wrapper=client_wrapper)
        self.accounts = AccountsClient(client_wrapper=client_wrapper)

        self.programs = ProgramsClient(client_wrapper=client_wrapper)

        self.rewards = RewardsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawLoyaltyClient
        """
        return self._raw_client

    def search_events(
        self,
        *,
        query: typing.Optional[LoyaltyEventQueryParams] = OMIT,
        limit: typing.Optional[int] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SearchLoyaltyEventsResponse:
        """
        Searches for loyalty events.

        A Square loyalty program maintains a ledger of events that occur during the lifetime of a
        buyer's loyalty account. Each change in the point balance
        (for example, points earned, points redeemed, and points expired) is
        recorded in the ledger. Using this endpoint, you can search the ledger for events.

        Search results are sorted by `created_at` in descending order.

        Parameters
        ----------
        query : typing.Optional[LoyaltyEventQueryParams]
            A set of one or more predefined query filters to apply when
            searching for loyalty events. The endpoint performs a logical AND to
            evaluate multiple filters and performs a logical OR on arrays
            that specifies multiple field values.

        limit : typing.Optional[int]
            The maximum number of results to include in the response.
            The last page might contain fewer events.
            The default is 30 events.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this to retrieve the next set of results for your original query.
            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

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

        Returns
        -------
        SearchLoyaltyEventsResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.loyalty.search_events(
            query={
                "filter": {
                    "order_filter": {"order_id": "PyATxhYLfsMqpVkcKJITPydgEYfZY"}
                }
            },
            limit=30,
        )
        """
        response = self._raw_client.search_events(
            query=query, limit=limit, cursor=cursor, request_options=request_options
        )
        return response.data


class AsyncLoyaltyClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawLoyaltyClient(client_wrapper=client_wrapper)
        self.accounts = AsyncAccountsClient(client_wrapper=client_wrapper)

        self.programs = AsyncProgramsClient(client_wrapper=client_wrapper)

        self.rewards = AsyncRewardsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawLoyaltyClient
        """
        return self._raw_client

    async def search_events(
        self,
        *,
        query: typing.Optional[LoyaltyEventQueryParams] = OMIT,
        limit: typing.Optional[int] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SearchLoyaltyEventsResponse:
        """
        Searches for loyalty events.

        A Square loyalty program maintains a ledger of events that occur during the lifetime of a
        buyer's loyalty account. Each change in the point balance
        (for example, points earned, points redeemed, and points expired) is
        recorded in the ledger. Using this endpoint, you can search the ledger for events.

        Search results are sorted by `created_at` in descending order.

        Parameters
        ----------
        query : typing.Optional[LoyaltyEventQueryParams]
            A set of one or more predefined query filters to apply when
            searching for loyalty events. The endpoint performs a logical AND to
            evaluate multiple filters and performs a logical OR on arrays
            that specifies multiple field values.

        limit : typing.Optional[int]
            The maximum number of results to include in the response.
            The last page might contain fewer events.
            The default is 30 events.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this to retrieve the next set of results for your original query.
            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

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

        Returns
        -------
        SearchLoyaltyEventsResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.loyalty.search_events(
                query={
                    "filter": {
                        "order_filter": {"order_id": "PyATxhYLfsMqpVkcKJITPydgEYfZY"}
                    }
                },
                limit=30,
            )


        asyncio.run(main())
        """
        response = await self._raw_client.search_events(
            query=query, limit=limit, cursor=cursor, request_options=request_options
        )
        return response.data
