"""cellpointapi.

This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
"""

# ruff: noqa: D410, E501, E101, D206
from apimatic_core.authentication.multiple.or_auth_group import (
    Or,
)
from apimatic_core.authentication.multiple.single_auth import (
    Single,
)
from apimatic_core.request_builder import RequestBuilder
from apimatic_core.response_handler import ResponseHandler
from apimatic_core.types.parameter import Parameter

from cellpointapi.api_helper import APIHelper
from cellpointapi.apis.base_api import BaseApi
from cellpointapi.configuration import Server
from cellpointapi.exceptions.api_exception import (
    ApiException,
)
from cellpointapi.exceptions.problem_exception import (
    ProblemException,
)
from cellpointapi.http.http_method_enum import (
    HttpMethodEnum,
)
from cellpointapi.models.order import Order
from cellpointapi.models.order_core import OrderCore
from cellpointapi.models.order_payment_summary import (
    OrderPaymentSummary,
)
from cellpointapi.models.order_transactions import (
    OrderTransactions,
)
from cellpointapi.models.refund_order_group_post_201_response import (
    RefundOrderGroupPost201Response,
)


class OrdersApi(BaseApi):
    """A Controller to access Endpoints in the cellpointapi API."""

    def __init__(self, config):
        """Initialize OrdersApi object."""
        super(OrdersApi, self).__init__(config)

    def create_order(self,
                     idempotency_key=None,
                     body=None):
        """Perform a POST request to /payments/v1/orders.

        Creates a new order.

        Args:
            idempotency_key (uuid|str, optional): A unique identifier.  If you don't
                receive a response (for example, in case of a timeout), you can
                safely retry the request with the same header.  If the system already
                processed the request, the response to the first attempt will be
                returned without duplication.  Idempotency-Keys will last for 24hrs.
                After this time, please do a GET call to the resource to check the
                status.  > This is used only in `POST` requests, so they have the
                same idempotency guarantees as `PUT`.
            body (Order, optional): Order request body.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. Created

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders")
            .http_method(HttpMethodEnum.POST)
            .header_param(Parameter()
                .key("Content-Type")
                .value("application/json"))
            .header_param(Parameter()
                .key("Idempotency-Key")
                .value(idempotency_key))
            .body_param(Parameter()
                .value(body))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .body_serializer(APIHelper.json_serialize)
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(OrderCore.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("403", "Forbidden", ProblemException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()

    def get_order_by_id(self,
                        order_id):
        """Perform a GET request to /payments/v1/orders/{orderId}.

        Retrieve a specific order

        Args:
            order_id (str): Order to retrieve

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. OK

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders/{orderId}")
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                .key("orderId")
                .value(order_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .auth(Or(Single("cpdIssuedJWT"), Single("cpdIssuedJWT"))),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(Order.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("404", "Resource Not Found", ApiException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()

    def get_order_summary_by_id(self,
                                order_id,
                                if_none_match=None):
        """Perform a GET request to /payments/v1/orders/{orderId}/summary.

        Retrieve the status and summary of a specific order.
        Same information can be retrieved from the callback of [Create
        Order](#tag/Orders/operation/create-order).
        This can be polled as an alternative to that callback.
        The information from the endpoint will change when the `order.status` has
        changed. Changes to transactions within an order will not cause a
        new version of this resource to be available.
        As such, do not use the endpoint to get real-time information about an active
        order that a buyer is attempting to pay for.
        The status will only change once the order is fully paid and has transitioned
        to the `PAID` state.
        For polling, we recommend using the `If-None-Match` header and sending the
        ETag, so a `304` will be responded with if the resource has not changed.

        Args:
            order_id (str): Order to retrieve status of.
            if_none_match (date, optional): The `If-None-Match` HTTP request header
                makes the request conditional. For `GET` and `HEAD` methods, the
                server will return the requested resource, with a `200` status, only
                if it doesn't have an `ETag` matching the given ones. For other
                methods, the request will be processed only if the eventually
                existing resource's `ETag` doesn't match any of the values listed.
                When the condition fails for `GET` and `HEAD` methods, then the
                server must return HTTP status code `304` (Not Modified). For methods
                that apply server-side changes, the status code `412` (Precondition
                Failed) is used. Note that the server generating a `304` response
                MUST generate any of the following header fields that would have been
                sent in a `200` (OK) response to the same request: Cache-Control,
                Content-Location, Date, ETag, Expires, and Vary.  The comparison with
                the stored `ETag` uses the weak comparison algorithm, meaning two
                files are considered identical if the content is equivalent — they
                don't have to be identical byte by byte. For example, two pages that
                differ by their creation date in the footer would still be considered
                identical.  When used in combination with `If-Modified-Since`,
                `If-None-Match` has precedence (if the server supports it).  There
                are two common use cases:    - For `GET` and `HEAD` methods, to
                update a cached entity that has an associated ETag.   - For other
                methods, and in particular for `PUT`,     `If-None-Match` used with
                the * value can be used to save a file not known to exist,
                guaranteeing that another upload didn't happen before, losing the
                data of the previous put;     this problem is a variation of the lost
                update problem.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. OK

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders/{orderId}/summary")
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                .key("orderId")
                .value(order_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("If-None-Match")
                .value(if_none_match))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .auth(Or(Single("cpdIssuedJWT"), Single("cpdIssuedJWT"))),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(OrderPaymentSummary.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("404", "Resource Not Found", ApiException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()

    def orders_transactions(self,
                            order_id):
        """Perform a GET request to /payments/v1/orders/{orderId}/transactions.

        Retrieve all transactions connected to an order.
        A transaction includes:
          - Payments
          - Refunds
          - Payouts

        Args:
            order_id (str): Order to retrieve all transactions for.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. OK

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders/{orderId}/transactions")
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                .key("orderId")
                .value(order_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(OrderTransactions.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException),
        ).execute()

    def fulfill_order(self,
                      order_id,
                      idempotency_key=None,
                      body=None):
        """Perform a POST request to
        /payments/v1/orders/{orderId}/fulfillments.

        Fulfills a completed order.

        Args:
            order_id (str): Order to fulfill.
            idempotency_key (uuid|str, optional): A unique identifier.  If you don't
                receive a response (for example, in case of a timeout), you can
                safely retry the request with the same header.  If the system already
                processed the request, the response to the first attempt will be
                returned without duplication.  Idempotency-Keys will last for 24hrs.
                After this time, please do a GET call to the resource to check the
                status.  > This is used only in `POST` requests, so they have the
                same idempotency guarantees as `PUT`.
            body (FulfilledOrder, optional): Order fulfillment request body.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. OK

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders/{orderId}/fulfillments")
            .http_method(HttpMethodEnum.POST)
            .template_param(Parameter()
                .key("orderId")
                .value(order_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("Content-Type")
                .value("application/json"))
            .header_param(Parameter()
                .key("Idempotency-Key")
                .value(idempotency_key))
            .body_param(Parameter()
                .value(body))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .body_serializer(APIHelper.json_serialize)
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(OrderPaymentSummary.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("403", "Forbidden", ProblemException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()

    def confirm_payments(self,
                         order_id,
                         idempotency_key=None):
        """Perform a POST request to
        /payments/v1/orders/{orderId}/confirm-payments.

        Before the payment can transition to a PAID state, the terms and conditions
        must be accepted.
        Each payment can mark itself as accepting those terms, but if the payments
        weren't marked so,
        an additional call to this endpoint would be required after all the payments
        have been made.

        Args:
            order_id (str): Order to accept Terms and Conditions for
            idempotency_key (uuid|str, optional): A unique identifier.  If you don't
                receive a response (for example, in case of a timeout), you can
                safely retry the request with the same header.  If the system already
                processed the request, the response to the first attempt will be
                returned without duplication.  Idempotency-Keys will last for 24hrs.
                After this time, please do a GET call to the resource to check the
                status.  > This is used only in `POST` requests, so they have the
                same idempotency guarantees as `PUT`.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. Accepted

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders/{orderId}/confirm-payments")
            .http_method(HttpMethodEnum.POST)
            .template_param(Parameter()
                .key("orderId")
                .value(order_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("Idempotency-Key")
                .value(idempotency_key))
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException),
        ).execute()

    def create_fingerprint(self,
                           order_id,
                           idempotency_key=None,
                           body=None):
        """Perform a POST request to
        /payments/v1/orders/{orderId}/fingerprints.

        Creates a new fingerprint for a buyer.

        Args:
            order_id (str): OrderId to save the fingerprint against.
            idempotency_key (uuid|str, optional): A unique identifier.  If you don't
                receive a response (for example, in case of a timeout), you can
                safely retry the request with the same header.  If the system already
                processed the request, the response to the first attempt will be
                returned without duplication.  Idempotency-Keys will last for 24hrs.
                After this time, please do a GET call to the resource to check the
                status.  > This is used only in `POST` requests, so they have the
                same idempotency guarantees as `PUT`.
            body (BuyerFingerprint, optional): Buyer Fingerprint to be saved.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. No Content

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/orders/{orderId}/fingerprints")
            .http_method(HttpMethodEnum.POST)
            .template_param(Parameter()
                .key("orderId")
                .value(order_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("Content-Type")
                .value("application/json"))
            .header_param(Parameter()
                .key("Idempotency-Key")
                .value(idempotency_key))
            .body_param(Parameter()
                .value(body))
            .body_serializer(APIHelper.json_serialize)
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("403", "Forbidden", ProblemException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()

    def create_connected_order(self,
                               order_group_id,
                               idempotency_key=None,
                               body=None):
        """Perform a POST request to
        /payments/v1/order-groups/{orderGroupId}/orders.

        Creates an order connected to other orders within an existing Order Group.
        An Order Group is made by passing an `orderGroupId` when [creating an
        order](#tag/Orders/operation/create-order).

        Args:
            order_group_id (str): Order group in which to make the connected order.
            idempotency_key (uuid|str, optional): A unique identifier.  If you don't
                receive a response (for example, in case of a timeout), you can
                safely retry the request with the same header.  If the system already
                processed the request, the response to the first attempt will be
                returned without duplication.  Idempotency-Keys will last for 24hrs.
                After this time, please do a GET call to the resource to check the
                status.  > This is used only in `POST` requests, so they have the
                same idempotency guarantees as `PUT`.
            body (Order, optional): Order request body.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. Created

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/order-groups/{orderGroupId}/orders")
            .http_method(HttpMethodEnum.POST)
            .template_param(Parameter()
                .key("orderGroupId")
                .value(order_group_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("Content-Type")
                .value("application/json"))
            .header_param(Parameter()
                .key("Idempotency-Key")
                .value(idempotency_key))
            .body_param(Parameter()
                .value(body))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .body_serializer(APIHelper.json_serialize)
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(OrderCore.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("403", "Forbidden", ProblemException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()

    def refund_order_group(self,
                           order_group_id,
                           idempotency_key=None,
                           body=None):
        """Perform a POST request to
        /payments/v1/order-groups/{orderGroupId}/refunds.

        Refunds an entire order group.
        An Order Group is made by passing an `orderGroupId` when [creating an
        order](#tag/Orders/operation/create-order).

        Args:
            order_group_id (str): Order to fulfill.
            idempotency_key (uuid|str, optional): A unique identifier.  If you don't
                receive a response (for example, in case of a timeout), you can
                safely retry the request with the same header.  If the system already
                processed the request, the response to the first attempt will be
                returned without duplication.  Idempotency-Keys will last for 24hrs.
                After this time, please do a GET call to the resource to check the
                status.  > This is used only in `POST` requests, so they have the
                same idempotency guarantees as `PUT`.
            body (RefundRequest, optional): Refund request body.

        Returns:
            ApiResponse: An object with the response value as well as other useful
                information such as status codes and headers. Created

        Raises:
            ApiException: When an error occurs while fetching the data from the
                remote API. This exception includes the HTTP Response code, an error
                message, and the HTTP body that was received in the request.

        """
        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path("/payments/v1/order-groups/{orderGroupId}/refunds")
            .http_method(HttpMethodEnum.POST)
            .template_param(Parameter()
                .key("orderGroupId")
                .value(order_group_id)
                .is_required(True)
                .should_encode(True))
            .header_param(Parameter()
                .key("Content-Type")
                .value("application/json"))
            .header_param(Parameter()
                .key("Idempotency-Key")
                .value(idempotency_key))
            .body_param(Parameter()
                .value(body))
            .header_param(Parameter()
                .key("accept")
                .value("application/json"))
            .body_serializer(APIHelper.json_serialize)
            .auth(Single("cpdIssuedJWT")),
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(RefundOrderGroupPost201Response.from_dictionary)
            .is_api_response(True)
            .local_error("400", "Bad Request", ProblemException)
            .local_error("401", "Authorization Required", ApiException)
            .local_error("403", "Forbidden", ProblemException)
            .local_error("406",
                "Cannot generate content acceptable according to the `Accept` headers"
                " in the request",
                ProblemException)
            .local_error("500", "Internal Server Error", ProblemException)
            .local_error("502", "Bad Gateway", ApiException)
            .local_error("503", "Service Unavailable", ApiException),
        ).execute()
