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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...requests.gift_card_activity import GiftCardActivityParams
from ...core.request_options import RequestOptions
from ...core.http_response import HttpResponse
from ...types.create_gift_card_activity_response import CreateGiftCardActivityResponse
from ...core.serialization import convert_and_respect_annotation_metadata
from ...core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...core.client_wrapper import AsyncClientWrapper
from ...core.http_response import AsyncHttpResponse

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


class RawActivitiesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def create(
        self,
        *,
        idempotency_key: str,
        gift_card_activity: GiftCardActivityParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateGiftCardActivityResponse]:
        """
        Creates a gift card activity to manage the balance or state of a [gift card](entity:GiftCard).
        For example, create an `ACTIVATE` activity to activate a gift card with an initial balance before first use.

        Parameters
        ----------
        idempotency_key : str
            A unique string that identifies the `CreateGiftCardActivity` request.

        gift_card_activity : GiftCardActivityParams
            The activity to create for the gift card. This activity must specify `gift_card_id` or `gift_card_gan` for the target
            gift card, the `location_id` where the activity occurred, and the activity `type` along with the corresponding activity details.

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

        Returns
        -------
        HttpResponse[CreateGiftCardActivityResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/gift-cards/activities",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "gift_card_activity": convert_and_respect_annotation_metadata(
                    object_=gift_card_activity, annotation=GiftCardActivityParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateGiftCardActivityResponse,
                    construct_type(
                        type_=CreateGiftCardActivityResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncRawActivitiesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        idempotency_key: str,
        gift_card_activity: GiftCardActivityParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateGiftCardActivityResponse]:
        """
        Creates a gift card activity to manage the balance or state of a [gift card](entity:GiftCard).
        For example, create an `ACTIVATE` activity to activate a gift card with an initial balance before first use.

        Parameters
        ----------
        idempotency_key : str
            A unique string that identifies the `CreateGiftCardActivity` request.

        gift_card_activity : GiftCardActivityParams
            The activity to create for the gift card. This activity must specify `gift_card_id` or `gift_card_gan` for the target
            gift card, the `location_id` where the activity occurred, and the activity `type` along with the corresponding activity details.

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

        Returns
        -------
        AsyncHttpResponse[CreateGiftCardActivityResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/gift-cards/activities",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "gift_card_activity": convert_and_respect_annotation_metadata(
                    object_=gift_card_activity, annotation=GiftCardActivityParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateGiftCardActivityResponse,
                    construct_type(
                        type_=CreateGiftCardActivityResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
