from __future__ import print_function

import logging

from django.conf import settings
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from lms.djangoapps.edraak_sendinblue.configurations import setup_sendinblue_configuration


log = logging.getLogger(__name__)


def create_contact(username, email, name, blacklisted):
    configuration = setup_sendinblue_configuration()

    if configuration:
        api_instance = sib_api_v3_sdk.ContactsApi(sib_api_v3_sdk.ApiClient(configuration))
        contact = sib_api_v3_sdk.CreateContact(
          email=email,
          attributes={
              "FULL_NAME": name
          },
          email_blacklisted=blacklisted,
          list_ids=[settings.EDRAAK_SENDINBLUE_LISTID, ],
          update_enabled=True
        )

        try:
            response = api_instance.create_contact(contact)
            log.info('SendInBlue contact created for user {username}, response text {text}'.format(username=username, text=response))
        except ApiException as e:
            log.exception("Exception when calling SendInBlue ContactsApi->create_contact: %s\n" % e)


# TODO: update contact attributes upon completion of profile
# TODO: delete a contact and integrate with user retirement
@@ -905,28 +905,12 @@
 ted 
-for user %7Busername%7D,
+with
  res
@@ -939,27 +939,8 @@
 mat(
-username=username, 
 text
@@ -1045,16 +1045,31 @@
 ndInBlue
+ for email (%7B%7D)
  Contact
@@ -1094,18 +1094,31 @@
 ct: 
-%25s%5Cn%22 %25
+%7B%7D%5Cn%22.format(email,
  e)
+)
 %0A%0A%0A#

stop using unsername in logging to solve old arabic usernames issue