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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..types.sort_order import SortOrder
from ..core.request_options import RequestOptions
from ..core.http_response import HttpResponse
from ..types.v1order import V1Order
from ..core.jsonable_encoder import jsonable_encoder
from ..core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.v1update_order_request_action import V1UpdateOrderRequestAction
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 RawV1TransactionsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def v1list_orders(
        self,
        location_id: str,
        *,
        order: typing.Optional[SortOrder] = None,
        limit: typing.Optional[int] = None,
        batch_token: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.List[V1Order]]:
        """
        Provides summary information for a merchant's online store orders.

        Parameters
        ----------
        location_id : str
            The ID of the location to list online store orders for.

        order : typing.Optional[SortOrder]
            The order in which payments are listed in the response.

        limit : typing.Optional[int]
            The maximum number of payments to return in a single response. This value cannot exceed 200.

        batch_token : typing.Optional[str]
            A pagination cursor to retrieve the next set of results for your
            original query to the endpoint.

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

        Returns
        -------
        HttpResponse[typing.List[V1Order]]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/{jsonable_encoder(location_id)}/orders",
            method="GET",
            params={
                "order": order,
                "limit": limit,
                "batch_token": batch_token,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[V1Order],
                    construct_type(
                        type_=typing.List[V1Order],  # 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)

    def v1retrieve_order(
        self, location_id: str, order_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[V1Order]:
        """
        Provides comprehensive information for a single online store order, including the order's history.

        Parameters
        ----------
        location_id : str
            The ID of the order's associated location.

        order_id : str
            The order's Square-issued ID. You obtain this value from Order objects returned by the List Orders endpoint

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

        Returns
        -------
        HttpResponse[V1Order]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/{jsonable_encoder(location_id)}/orders/{jsonable_encoder(order_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    V1Order,
                    construct_type(
                        type_=V1Order,  # 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)

    def v1update_order(
        self,
        location_id: str,
        order_id: str,
        *,
        action: V1UpdateOrderRequestAction,
        shipped_tracking_number: typing.Optional[str] = OMIT,
        completed_note: typing.Optional[str] = OMIT,
        refunded_note: typing.Optional[str] = OMIT,
        canceled_note: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[V1Order]:
        """
        Updates the details of an online store order. Every update you perform on an order corresponds to one of three actions:

        Parameters
        ----------
        location_id : str
            The ID of the order's associated location.

        order_id : str
            The order's Square-issued ID. You obtain this value from Order objects returned by the List Orders endpoint

        action : V1UpdateOrderRequestAction
            The action to perform on the order (COMPLETE, CANCEL, or REFUND).
            See [V1UpdateOrderRequestAction](#type-v1updateorderrequestaction) for possible values

        shipped_tracking_number : typing.Optional[str]
            The tracking number of the shipment associated with the order. Only valid if action is COMPLETE.

        completed_note : typing.Optional[str]
            A merchant-specified note about the completion of the order. Only valid if action is COMPLETE.

        refunded_note : typing.Optional[str]
            A merchant-specified note about the refunding of the order. Only valid if action is REFUND.

        canceled_note : typing.Optional[str]
            A merchant-specified note about the canceling of the order. Only valid if action is CANCEL.

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

        Returns
        -------
        HttpResponse[V1Order]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/{jsonable_encoder(location_id)}/orders/{jsonable_encoder(order_id)}",
            method="PUT",
            json={
                "action": action,
                "shipped_tracking_number": shipped_tracking_number,
                "completed_note": completed_note,
                "refunded_note": refunded_note,
                "canceled_note": canceled_note,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    V1Order,
                    construct_type(
                        type_=V1Order,  # 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 AsyncRawV1TransactionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def v1list_orders(
        self,
        location_id: str,
        *,
        order: typing.Optional[SortOrder] = None,
        limit: typing.Optional[int] = None,
        batch_token: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.List[V1Order]]:
        """
        Provides summary information for a merchant's online store orders.

        Parameters
        ----------
        location_id : str
            The ID of the location to list online store orders for.

        order : typing.Optional[SortOrder]
            The order in which payments are listed in the response.

        limit : typing.Optional[int]
            The maximum number of payments to return in a single response. This value cannot exceed 200.

        batch_token : typing.Optional[str]
            A pagination cursor to retrieve the next set of results for your
            original query to the endpoint.

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

        Returns
        -------
        AsyncHttpResponse[typing.List[V1Order]]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/{jsonable_encoder(location_id)}/orders",
            method="GET",
            params={
                "order": order,
                "limit": limit,
                "batch_token": batch_token,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[V1Order],
                    construct_type(
                        type_=typing.List[V1Order],  # 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)

    async def v1retrieve_order(
        self, location_id: str, order_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[V1Order]:
        """
        Provides comprehensive information for a single online store order, including the order's history.

        Parameters
        ----------
        location_id : str
            The ID of the order's associated location.

        order_id : str
            The order's Square-issued ID. You obtain this value from Order objects returned by the List Orders endpoint

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

        Returns
        -------
        AsyncHttpResponse[V1Order]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/{jsonable_encoder(location_id)}/orders/{jsonable_encoder(order_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    V1Order,
                    construct_type(
                        type_=V1Order,  # 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)

    async def v1update_order(
        self,
        location_id: str,
        order_id: str,
        *,
        action: V1UpdateOrderRequestAction,
        shipped_tracking_number: typing.Optional[str] = OMIT,
        completed_note: typing.Optional[str] = OMIT,
        refunded_note: typing.Optional[str] = OMIT,
        canceled_note: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[V1Order]:
        """
        Updates the details of an online store order. Every update you perform on an order corresponds to one of three actions:

        Parameters
        ----------
        location_id : str
            The ID of the order's associated location.

        order_id : str
            The order's Square-issued ID. You obtain this value from Order objects returned by the List Orders endpoint

        action : V1UpdateOrderRequestAction
            The action to perform on the order (COMPLETE, CANCEL, or REFUND).
            See [V1UpdateOrderRequestAction](#type-v1updateorderrequestaction) for possible values

        shipped_tracking_number : typing.Optional[str]
            The tracking number of the shipment associated with the order. Only valid if action is COMPLETE.

        completed_note : typing.Optional[str]
            A merchant-specified note about the completion of the order. Only valid if action is COMPLETE.

        refunded_note : typing.Optional[str]
            A merchant-specified note about the refunding of the order. Only valid if action is REFUND.

        canceled_note : typing.Optional[str]
            A merchant-specified note about the canceling of the order. Only valid if action is CANCEL.

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

        Returns
        -------
        AsyncHttpResponse[V1Order]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/{jsonable_encoder(location_id)}/orders/{jsonable_encoder(order_id)}",
            method="PUT",
            json={
                "action": action,
                "shipped_tracking_number": shipped_tracking_number,
                "completed_note": completed_note,
                "refunded_note": refunded_note,
                "canceled_note": canceled_note,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    V1Order,
                    construct_type(
                        type_=V1Order,  # 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)
