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

import requests as requests_http
from . import utils
from sdk.models import operations, shared
from typing import Optional

class Customers:
    r"""Everything about Customer collection
    https://doc.getlago.com/docs/api/customers/customer-object - Find out more
    """
    _client: requests_http.Session
    _security_client: requests_http.Session
    _server_url: str
    _language: str
    _sdk_version: str
    _gen_version: str

    def __init__(self, client: requests_http.Session, security_client: requests_http.Session, server_url: str, language: str, sdk_version: str, gen_version: str) -> None:
        self._client = client
        self._security_client = security_client
        self._server_url = server_url
        self._language = language
        self._sdk_version = sdk_version
        self._gen_version = gen_version
        
    def create(self, request: shared.CustomerInput) -> operations.CreateCustomerResponse:
        r"""Create a customer
        Create a new customer
        """
        base_url = self._server_url
        
        url = base_url.removesuffix('/') + '/customers'
        
        headers = {}
        req_content_type, data, form = utils.serialize_request_body(request, "request", 'json')
        if req_content_type not in ('multipart/form-data', 'multipart/mixed'):
            headers['content-type'] = req_content_type
        if data is None and form is None:
            raise Exception('request body is required')
        
        client = self._security_client
        
        http_res = client.request('POST', url, data=data, files=form, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.CreateCustomerResponse(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.Customer])
                res.customer = out
        elif http_res.status_code == 400:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseBadRequest])
                res.api_response_bad_request = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out
        elif http_res.status_code == 422:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnprocessableEntity])
                res.api_response_unprocessable_entity = out

        return res

    def current_usage(self, request: operations.FindCustomerCurrentUsageRequest) -> operations.FindCustomerCurrentUsageResponse:
        r"""Find customer current usage
        Return a customer current usage
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.FindCustomerCurrentUsageRequest, base_url, '/customers/{customer_external_id}/current_usage', request)
        
        query_params = utils.get_query_params(operations.FindCustomerCurrentUsageRequest, request)
        
        client = self._security_client
        
        http_res = client.request('GET', url, params=query_params)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FindCustomerCurrentUsageResponse(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.CustomerUsage])
                res.customer_usage = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseNotFound])
                res.api_response_not_found = out

        return res

    def delete_applied_coupon(self, request: operations.DeleteAppliedCouponRequest) -> operations.DeleteAppliedCouponResponse:
        r"""Delete customer's appplied coupon
        Delete customer's appplied coupon
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.DeleteAppliedCouponRequest, base_url, '/customers/{customer_external_id}/applied_coupons/{applied_coupon_id}', request)
        
        
        client = self._security_client
        
        http_res = client.request('DELETE', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.DeleteAppliedCouponResponse(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.AppliedCoupon])
                res.applied_coupon = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseNotFound])
                res.api_response_not_found = out

        return res

    def destroy(self, request: operations.DeleteCustomerRequest) -> operations.DeleteCustomerResponse:
        r"""Delete a customer
        Return the deleted customer
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.DeleteCustomerRequest, base_url, '/customers/{external_id}', request)
        
        
        client = self._security_client
        
        http_res = client.request('DELETE', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.DeleteCustomerResponse(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.Customer])
                res.customer = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseNotFound])
                res.api_response_not_found = out

        return res

    def find(self, request: operations.FindCustomerRequest) -> operations.FindCustomerResponse:
        r"""Find customer by external ID
        Return a single customer
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.FindCustomerRequest, base_url, '/customers/{external_id}', request)
        
        
        client = self._security_client
        
        http_res = client.request('GET', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FindCustomerResponse(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.Customer])
                res.customer = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseNotFound])
                res.api_response_not_found = out

        return res

    def find_all(self, request: operations.FindAllCustomersRequest) -> operations.FindAllCustomersResponse:
        r"""Find customers
        Find all customers in certain organisation
        """
        base_url = self._server_url
        
        url = base_url.removesuffix('/') + '/customers'
        
        query_params = utils.get_query_params(operations.FindAllCustomersRequest, request)
        
        client = self._security_client
        
        http_res = client.request('GET', url, params=query_params)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FindAllCustomersResponse(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.Customers])
                res.customers = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out

        return res

    def portal_url(self, request: operations.GetCustomerPortalURLRequest) -> operations.GetCustomerPortalURLResponse:
        r"""Get customer portal URL
        Get customer portal URL
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.GetCustomerPortalURLRequest, base_url, '/customers/{customer_external_id}/portal_url', request)
        
        
        client = self._security_client
        
        http_res = client.request('GET', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.GetCustomerPortalURLResponse(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[operations.GetCustomerPortalURL200ApplicationJSON])
                res.get_customer_portal_url_200_application_json_object = out
        elif http_res.status_code == 401:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseUnauthorized])
                res.api_response_unauthorized = out
        elif http_res.status_code == 403:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseForbidden])
                res.api_response_forbidden = out
        elif http_res.status_code == 404:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseNotFound])
                res.api_response_not_found = out

        return res

    