"""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 Invoices:
    r"""Everything about Invoice collection
    https://doc.getlago.com/docs/api/invoices/invoice-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 download(self, request: operations.DownloadInvoiceRequest) -> operations.DownloadInvoiceResponse:
        r"""Download an existing invoice
        Download an existing invoice
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.DownloadInvoiceRequest, base_url, '/invoices/{id}/download', request)
        
        
        client = self._security_client
        
        http_res = client.request('POST', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.DownloadInvoiceResponse(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.Invoice])
                res.invoice = 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 finalize(self, request: operations.FinalizeInvoiceRequest) -> operations.FinalizeInvoiceResponse:
        r"""Finalize a draft invoice
        Finalize a draft invoice
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.FinalizeInvoiceRequest, base_url, '/invoices/{id}/finalize', request)
        
        
        client = self._security_client
        
        http_res = client.request('PUT', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FinalizeInvoiceResponse(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.Invoice])
                res.invoice = 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.FindInvoiceRequest) -> operations.FindInvoiceResponse:
        r"""Find invoice by ID
        Return a single invoice
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.FindInvoiceRequest, base_url, '/invoices/{id}', request)
        
        
        client = self._security_client
        
        http_res = client.request('GET', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FindInvoiceResponse(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.Invoice])
                res.invoice = 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.FindAllInvoicesRequest) -> operations.FindAllInvoicesResponse:
        r"""Find all invoices
        Find all invoices in certain organisation
        """
        base_url = self._server_url
        
        url = base_url.removesuffix('/') + '/invoices'
        
        query_params = utils.get_query_params(operations.FindAllInvoicesRequest, request)
        
        client = self._security_client
        
        http_res = client.request('GET', url, params=query_params)
        content_type = http_res.headers.get('Content-Type')

        res = operations.FindAllInvoicesResponse(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.Invoices])
                res.invoices = 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 retry(self, request: operations.RetryPaymentRequest) -> operations.RetryPaymentResponse:
        r"""Retry invoice payment
        Retry invoice payment
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.RetryPaymentRequest, base_url, '/invoices/{id}/retry_payment', request)
        
        
        client = self._security_client
        
        http_res = client.request('POST', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.RetryPaymentResponse(status_code=http_res.status_code, content_type=content_type, raw_response=http_res)
        
        if http_res.status_code == 200:
            pass
        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
        elif http_res.status_code == 405:
            if utils.match_content_type(content_type, 'application/json'):
                out = utils.unmarshal_json(http_res.text, Optional[shared.APIResponseNotAllowed])
                res.api_response_not_allowed = out

        return res

    def update(self, request: operations.UpdateInvoiceRequest) -> operations.UpdateInvoiceResponse:
        r"""Update an existing invoice status
        Update an existing invoice
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.UpdateInvoiceRequest, base_url, '/invoices/{id}', request)
        
        headers = {}
        req_content_type, data, form = utils.serialize_request_body(request, "invoice_input", '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('PUT', url, data=data, files=form, headers=headers)
        content_type = http_res.headers.get('Content-Type')

        res = operations.UpdateInvoiceResponse(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.Invoice])
                res.invoice = 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 == 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
        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 void(self, request: operations.RefreshInvoiceRequest) -> operations.RefreshInvoiceResponse:
        r"""Refresh a draft invoice
        Refresh a draft invoice
        """
        base_url = self._server_url
        
        url = utils.generate_url(operations.RefreshInvoiceRequest, base_url, '/invoices/{id}/refresh', request)
        
        
        client = self._security_client
        
        http_res = client.request('PUT', url)
        content_type = http_res.headers.get('Content-Type')

        res = operations.RefreshInvoiceResponse(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.Invoice])
                res.invoice = 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

    