from user.models import User
import requests

# def try_pay(amount: int, currency: str, payment_method: str, user_id: str):
#     try:
#         # Use Stripe's library to make requests...
#         stripe.api_key = "sk_test_51PpsMl2Kr9WuyfidyblSXtjw501giQgOC4NVdBLA2JX7wYTsE6WX0O01feSDIqXLbBXHwkZFul6hKFtqQEMgH5pZ00QYEOcuR2"
#         charge = stripe.Charge.retrieve(
#         'ch_3Ln0cK2eZvKYlo2C1QmvaARY','prod_RxBhvgU9rLzwaY'
#         expand=['customer', 'invoice.subscription']
#         )
#         pass
#     except stripe.error.CardError as e:
#         # Since it's a decline, stripe.error.CardError will be caught
#         print('Status is: %s' % e.http_status)
#         print('Code is: %s' % e.code)
#         # param is '' in this case
#         print('Param is: %s' % e.param)
#         print('Message is: %s' % e.user_message)
#     except stripe.error.RateLimitError as e:
#         # Too many requests made to the API too quickly
#         pass
#     except stripe.error.InvalidRequestError as e:
#         # Invalid parameters were supplied to Stripe's API
#         pass
#     except stripe.error.AuthenticationError as e:
#         # Authentication with Stripe's API failed
#         # (maybe you changed API keys recently)
#         pass
#     except stripe.error.APIConnectionError as e:
#         # Network communication with Stripe failed
#         pass
#     except stripe.error.StripeError as e:
#         # Display a very generic error to the user, and maybe send
#         # yourself an email
#         pass
#     except Exception as e:
#         # Something else happened, completely unrelated to Stripe
#         pass


def create_client_and_save_bank_id(first_name: str, last_name: str, email: str, country_code: str = "FR"):
    url = "https://thirdparty.qonto.com/v2/clients"
    headers = {
        "Accept": "application/json",
        "Authorization": "ed-disce-2209:3564faed2a2ee96aad0e9b16083d323a",
        "X-Qonto-Staging-Token": ""
    }
    body = {
        "first_name": first_name,
        "last_name": last_name,
        "type": "individual",
        "email": email,
        "country_code": country_code,
        "currency": "EUR",
        "locale": "FR"
    }
    response = requests.post(url, headers=headers, json=body)
    client = response.json()['client']
    bank_id = client['bank_id']
    user = User.objects.get(email=email)
    user.bank_id = bank_id
    user.save()

    return client


def get_client_list():
    url = "https://thirdparty.qonto.com/v2/clients"
    headers = {
        "Accept": "application/json",
        "Authorization": "ed-disce-2209:3564faed2a2ee96aad0e9b16083d323a",
        "X-Qonto-Staging-Token": ""
    }
    response = requests.get(url, headers=headers)
    return response.json()


def get_client(client_id: str):
    url = f"https://thirdparty.qonto.com/v2/clients/{client_id}"
    headers = {
        "Accept": "application/json",
        "Authorization": "ed-disce-2209:3564faed2a2ee96aad0e9b16083d323a",
        "X-Qonto-Staging-Token": ""
    }
    response = requests.get(url, headers=headers)
    return response.json()
