"""Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT."""

from .sdkconfiguration import SDKConfiguration
from orb import utils
from orb.models import errors, operations, shared
from typing import Optional, Union

class Coupon:
    r"""A coupon represents a reusable discount configuration, and have an attached redemption code that can be issued to
    your end users. Coupons are most often used in self-serve signup or upgrade flows in your checkout experience or
    billing portal.

    To redeem a coupon, pass the `redemption_code` property in the [create subscription](create-subscription.api.mdx)
    or [schedule plan change](schedule-plan-change.api.mdx) request.
    """
    sdk_configuration: SDKConfiguration

    def __init__(self, sdk_config: SDKConfiguration) -> None:
        self.sdk_configuration = sdk_config
        
    
    def archive(self, coupon_id: str) -> operations.ArchiveCouponResponse:
        r"""Archive coupon
        This endpoint allows a coupon to be archived. Archived coupons can no longer be redeemed, and
        will be hidden from lists of active coupons. Additionally, once a coupon is archived, its redemption
        code can be reused for a different coupon.
        """
        request = operations.ArchiveCouponRequest(
            coupon_id=coupon_id,
        )
        
        base_url = utils.template_url(*self.sdk_configuration.get_server_details())
        
        url = utils.generate_url(operations.ArchiveCouponRequest, base_url, '/coupons/{coupon_id}/archive', request)
        headers = {}
        headers['Accept'] = 'application/json'
        headers['user-agent'] = self.sdk_configuration.user_agent
        
        client = self.sdk_configuration.security_client
        
        http_res = client.request('POST', url, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.ArchiveCouponResponse(status_code=http_res.status_code, content_type=content_type, raw_response=http_res)
        
        if http_res.status_code == 200:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.Coupon])
                res.coupon = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 400:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.DuplicateConstraintViolationError, shared.DuplicateResourceCreationError, shared.RequestValidationError]])
                res.four_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndOneError])
                res.four_hundred_and_one_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.FeatureNotAvailableError, shared.ResourceNotFoundError, shared.URLNotFound]])
                res.four_hundred_and_four_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 409:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.IdempotencyConflict, shared.IdempotencyKeyValidationError, shared.IdempotencyRequestMismatch]])
                res.four_hundred_and_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 413:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.RequestTooLargeError, shared.ResourceTooLargeError]])
                res.four_hundred_and_thirteen_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 429:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndTwentyNineError])
                res.four_hundred_and_twenty_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 500:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FiveHundredError])
                res.five_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)

        return res

    
    def create(self, request: shared.NewCoupon) -> operations.CreateCouponResponse:
        r"""Create coupon
        This endpoint allows the creation of coupons, which can then be redeemed at subscription creation or plan change.
        """
        base_url = utils.template_url(*self.sdk_configuration.get_server_details())
        
        url = base_url + '/coupons'
        headers = {}
        req_content_type, data, form = utils.serialize_request_body(request, "request", False, True, 'json')
        if req_content_type not in ('multipart/form-data', 'multipart/mixed'):
            headers['content-type'] = req_content_type
        headers['Accept'] = 'application/json'
        headers['user-agent'] = self.sdk_configuration.user_agent
        
        client = self.sdk_configuration.security_client
        
        http_res = client.request('POST', url, data=data, files=form, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.CreateCouponResponse(status_code=http_res.status_code, content_type=content_type, raw_response=http_res)
        
        if http_res.status_code == 201:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.Coupon])
                res.coupon = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 400:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.DuplicateConstraintViolationError, shared.DuplicateResourceCreationError, shared.RequestValidationError]])
                res.four_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndOneError])
                res.four_hundred_and_one_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.FeatureNotAvailableError, shared.ResourceNotFoundError, shared.URLNotFound]])
                res.four_hundred_and_four_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 409:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.IdempotencyConflict, shared.IdempotencyKeyValidationError, shared.IdempotencyRequestMismatch]])
                res.four_hundred_and_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 413:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.RequestTooLargeError, shared.ResourceTooLargeError]])
                res.four_hundred_and_thirteen_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 429:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndTwentyNineError])
                res.four_hundred_and_twenty_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 500:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FiveHundredError])
                res.five_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)

        return res

    
    def fetch(self, coupon_id: str) -> operations.FetchCouponResponse:
        r"""Fetch coupon
        This endpoint retrieves a coupon by its ID. To fetch coupons by their redemption code, use the
        [List coupons](list-coupons) endpoint with the redemption_code parameter.
        """
        request = operations.FetchCouponRequest(
            coupon_id=coupon_id,
        )
        
        base_url = utils.template_url(*self.sdk_configuration.get_server_details())
        
        url = utils.generate_url(operations.FetchCouponRequest, base_url, '/coupons/{coupon_id}', request)
        headers = {}
        headers['Accept'] = 'application/json'
        headers['user-agent'] = self.sdk_configuration.user_agent
        
        client = self.sdk_configuration.security_client
        
        http_res = client.request('GET', url, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FetchCouponResponse(status_code=http_res.status_code, content_type=content_type, raw_response=http_res)
        
        if http_res.status_code == 200:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.Coupon])
                res.coupon = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 400:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.DuplicateConstraintViolationError, shared.DuplicateResourceCreationError, shared.RequestValidationError]])
                res.four_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndOneError])
                res.four_hundred_and_one_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.FeatureNotAvailableError, shared.ResourceNotFoundError, shared.URLNotFound]])
                res.four_hundred_and_four_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 409:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.IdempotencyConflict, shared.IdempotencyKeyValidationError, shared.IdempotencyRequestMismatch]])
                res.four_hundred_and_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 413:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.RequestTooLargeError, shared.ResourceTooLargeError]])
                res.four_hundred_and_thirteen_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 429:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndTwentyNineError])
                res.four_hundred_and_twenty_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 500:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FiveHundredError])
                res.five_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)

        return res

    
    def list(self, cursor: Optional[str] = None, limit: Optional[int] = None, redemption_code: Optional[str] = None, show_archived: Optional[bool] = None) -> operations.ListCouponsResponse:
        r"""List coupons
        This endpoint returns a list of all coupons for an account in a list format.

        The list of coupons is ordered starting from the most recently created coupon. The response also includes
        `pagination_metadata`, which lets the caller retrieve the next page of results if they exist. More information
        about pagination can be found in the Pagination-metadata schema.
        """
        request = operations.ListCouponsRequest(
            cursor=cursor,
            limit=limit,
            redemption_code=redemption_code,
            show_archived=show_archived,
        )
        
        base_url = utils.template_url(*self.sdk_configuration.get_server_details())
        
        url = base_url + '/coupons'
        headers = {}
        query_params = utils.get_query_params(operations.ListCouponsRequest, request)
        headers['Accept'] = 'application/json'
        headers['user-agent'] = self.sdk_configuration.user_agent
        
        client = self.sdk_configuration.security_client
        
        http_res = client.request('GET', url, params=query_params, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.ListCouponsResponse(status_code=http_res.status_code, content_type=content_type, raw_response=http_res)
        
        if http_res.status_code == 200:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.Coupons])
                res.coupons = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 400:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.DuplicateConstraintViolationError, shared.DuplicateResourceCreationError, shared.RequestValidationError]])
                res.four_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndOneError])
                res.four_hundred_and_one_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.FeatureNotAvailableError, shared.ResourceNotFoundError, shared.URLNotFound]])
                res.four_hundred_and_four_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 409:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.IdempotencyConflict, shared.IdempotencyKeyValidationError, shared.IdempotencyRequestMismatch]])
                res.four_hundred_and_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 413:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.RequestTooLargeError, shared.ResourceTooLargeError]])
                res.four_hundred_and_thirteen_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 429:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndTwentyNineError])
                res.four_hundred_and_twenty_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 500:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FiveHundredError])
                res.five_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)

        return res

    
    def list_subscriptions(self, coupon_id: str, cursor: Optional[str] = None, limit: Optional[int] = None) -> operations.ListCouponSubscriptionsResponse:
        r"""List coupon subscriptions
        This endpoint returns a list of all subscriptions that have redeemed a given coupon as a
        [paginated](../reference/pagination) list, ordered starting from the most recently created subscription. For a full
        discussion of the subscription resource, see [Subscription](../guides/concepts#subscription).
        """
        request = operations.ListCouponSubscriptionsRequest(
            coupon_id=coupon_id,
            cursor=cursor,
            limit=limit,
        )
        
        base_url = utils.template_url(*self.sdk_configuration.get_server_details())
        
        url = utils.generate_url(operations.ListCouponSubscriptionsRequest, base_url, '/coupons/{coupon_id}/subscriptions', request)
        headers = {}
        query_params = utils.get_query_params(operations.ListCouponSubscriptionsRequest, request)
        headers['Accept'] = 'application/json'
        headers['user-agent'] = self.sdk_configuration.user_agent
        
        client = self.sdk_configuration.security_client
        
        http_res = client.request('GET', url, params=query_params, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.ListCouponSubscriptionsResponse(status_code=http_res.status_code, content_type=content_type, raw_response=http_res)
        
        if http_res.status_code == 200:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.Subscriptions])
                res.subscriptions = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 400:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.DuplicateConstraintViolationError, shared.DuplicateResourceCreationError, shared.RequestValidationError]])
                res.four_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndOneError])
                res.four_hundred_and_one_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.FeatureNotAvailableError, shared.ResourceNotFoundError, shared.URLNotFound]])
                res.four_hundred_and_four_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 409:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.IdempotencyConflict, shared.IdempotencyKeyValidationError, shared.IdempotencyRequestMismatch]])
                res.four_hundred_and_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 413:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[Union[shared.RequestTooLargeError, shared.ResourceTooLargeError]])
                res.four_hundred_and_thirteen_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 429:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FourHundredAndTwentyNineError])
                res.four_hundred_and_twenty_nine_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
        elif http_res.status_code == 500:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.FiveHundredError])
                res.five_hundred_error = out
            else:
                raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)

        return res

    