from django.core.management.base import BaseCommand
from django.db import connection
from mailchimp_pipeline.client import ChimpClient
from mailchimp_pipeline.helpers import get_user_active_enrollements, get_enrollements_course_short_ids
from django.contrib.auth.models import User
from lms.djangoapps.onboarding.models import FocusArea, OrgSector
from lms.djangoapps.certificates import api as certificate_api
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from django.conf import settings

from logging import getLogger
log = getLogger(__name__)


class Command(BaseCommand):
    help = """
    One time addition of already existing users into mailchimp learner's list
    example:
        manage.py sync_users_with_mailchimp
    """

    def send_user_to_mailchimp(self, client, users):
        client.add_list_members_in_batch(settings.MAILCHIMP_LEARNERS_LIST_ID, {"members": users})

    def get_users_data_to_send(self, users):
        users_set = []

        focus_areas = FocusArea.get_map()
        org_sectors = OrgSector.get_map()

        for user in users:
            profile = user.profile
            extended_profile = user.extended_profile

            org_type = ""
            if extended_profile.organization and extended_profile.organization.org_type:
                org_type = org_sectors.get(extended_profile.organization.org_type, '')

            all_certs = []
            try:
                all_certs = certificate_api.get_certificates_for_user(user.username)
            except Exception as ex:
                log.exception(str(ex.args))

            completed_course_keys = [cert.get('course_key', '') for cert in all_certs
                                     if certificate_api.is_passing_status(cert['status'])]
            completed_courses = CourseOverview.objects.filter(id__in=completed_course_keys)

            try:
                user_json = {
                    "email_address": user.email,
                    "status_if_new": "subscribed",
                    "merge_fields": {
                        "FULLNAME": user.get_full_name(),
                        "USERNAME": user.username,
                        "LANG": profile.language if profile.language else "",
                        "COUNTRY": profile.country.name.format() if profile.country else "",
                        "CITY": profile.city if profile.city else "",
                        "DATEREGIS": str(user.date_joined.strftime("%m/%d/%Y")),
                        "LSOURCE": "",
                        "COMPLETES": ", ".join([course.display_name for course in completed_courses]),
                        "ENROLLS": get_user_active_enrollements(user.username),
                        "ENROLL_IDS": get_enrollements_course_short_ids(user.username),
                        "ORG": extended_profile.organization.label if extended_profile.organization else "",
                        "ORGTYPE": org_type,
                        "WORKAREA": str(focus_areas.get(extended_profile.organization.focus_area, ""))
                        if extended_profile.organization else "",
                    }
                }
            except Exception as ex:
                log.info("There was an error for user with email address as {}".format(user.email))
                log.exception(str(ex.args))
                continue

            users_set.append(user_json)

        return users_set

    def handle(self, *args, **options):
        batch_size = 500
        cursor = connection.cursor()
        cursor.execute('SET TRANSACTION ISOLATION LEVEL READ COMMITTED')
        client = ChimpClient()
        total_user_count = User.objects.all().count()
        page_count = total_user_count / batch_size
        counter = 0

        while counter is not page_count+1:
            try:
                page_start = counter * batch_size
                page_end = page_start + batch_size
                users = list(User.objects.all()[page_start:page_end])
                log.info(User.objects.all()[page_start:page_end].query)
                users_json = self.get_users_data_to_send(users)
                self.send_user_to_mailchimp(client, users_json)
            except Exception as ex:
                log.info("There was an error in batch from {} to {}".format(page_start, page_end))
                log.exception(str(ex.args))

            counter += 1


@@ -898,16 +898,29 @@
 ST_ID, %7B
+%0A            
 %22members
@@ -927,16 +927,62 @@
 %22: users
+,%0A            %22update_existing%22: True%0A        
 %7D)%0A%0A    
@@ -1158,16 +1158,164 @@
  users:%0A
+            language = country = city = organization = org_type = work_area = %22%22%0A            profile = extended_profile = None%0A            try:%0A    
         
@@ -1341,16 +1341,20 @@
 profile%0A
+    
         
@@ -1415,21 +1415,252 @@
     
-org_type = %22%22
+    if profile.language:%0A                    language = profile.language%0A%0A                if profile.country:%0A                    country = profile.country.name.format()%0A%0A                if profile.city:%0A                    city = profile.city%0A
 %0A   
@@ -1660,32 +1660,36 @@
 ty%0A%0A            
+    
 if extended_prof
@@ -1708,12 +1708,248 @@
 tion
- and
+:%0A                    organization = extended_profile.organization.label%0A                    work_area = str(focus_areas.get(%0A                        extended_profile.organization.focus_area, %22%22%0A                    ))%0A                    if
  ext
@@ -1985,16 +1985,24 @@
 g_type:%0A
+        
         
@@ -2032,24 +2032,53 @@
 sectors.get(
+%0A                            
 extended_pro
@@ -2107,17 +2107,83 @@
 type, ''
-)
+%0A                        )%0A            except:%0A                pass
 %0A%0A      
@@ -2986,215 +2986,98 @@
 G%22: 
-profile.language if profile.language else %22%22,%0A                        %22COUNTRY%22: profile.country.name.format() if profile.country else %22%22,%0A                        %22CITY%22: profile.city if profile.city else %22%22
+language,%0A                        %22COUNTRY%22: country,%0A                        %22CITY%22: city
 ,%0A  
@@ -3500,84 +3500,20 @@
 G%22: 
-extended_profile.organization.label if extended_profile.organization else %22%22
+organization
 ,%0A  
@@ -3595,139 +3595,17 @@
 A%22: 
-str(focus_areas.get(extended_profile.organization.focus_area, %22%22))%0A                        if extended_profile.organization else %22%22
+work_area
 ,%0A  

Handle RelatedObjectDoesNotExist exception