import logging

from website import mails
from django.utils import timezone

from framework.celery_tasks import app as celery_app
from website.app import setup_django
setup_django()
from osf.models import OSFUser
from website.settings import OSF_SUPPORT_EMAIL, OSF_CONTACT_EMAIL
from django.core.management.base import BaseCommand

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def deactivate_requested_accounts(dry_run=True):
    users = OSFUser.objects.filter(requested_deactivation=True, contacted_deactivation=False, date_disabled__isnull=True)

    for user in users:
        if user.has_resources:
            mails.send_mail(
                to_addr=OSF_SUPPORT_EMAIL,
                mail=mails.REQUEST_DEACTIVATION,
                user=user,
                can_change_preferences=False,
            )
        else:
            user.disable_account()
            user.is_registered = False
            mails.send_mail(
                to_addr=user.username,
                mail=mails.REQUEST_DEACTIVATION_COMPLETE,
                user=user,
                contact_email=OSF_CONTACT_EMAIL,
                can_change_preferences=False,
            )

        user.contacted_deactivation = True
        user.email_last_sent = timezone.now()
        if not dry_run:
            user.save()

    if dry_run:
        logger.info('Dry run complete')


@celery_app.task(name='management.commands.check_crossref_dois')
def main(dry_run=False):
    """
    This task runs nightly and emails users who want to delete there account with info on how to do so. Users who don't
    have any content can be automatically deleted.
    """

    deactivate_requested_accounts(dry_run=dry_run)


class Command(BaseCommand):
    help = '''
    If there are any users who want to be deactivated we will either: immediately deactivate, or if they have active
    resources (undeleted nodes, preprints etc) we contact admin to guide the user through the deactivation process.
    '''

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        parser.add_argument(
            '--dry',
            action='store_true',
            dest='dry_run',
            help='Dry run',
        )

    # Management command handler
    def handle(self, *args, **options):
        dry_run = options.get('dry_run', True)
        main(dry_run=dry_run)

@@ -630,16 +630,138 @@
 ources:%0A
+            logger.info('User %7B%7D was contacted for about deactivation.'.format(user._id))%0A            if not dry_run:%0A    
         
@@ -789,32 +789,36 @@
                 
+  
+  
 to_addr=OSF_SUPP
@@ -820,32 +820,36 @@
 _SUPPORT_EMAIL,%0A
+    
                 
@@ -889,32 +889,36 @@
                 
+  
+  
 user=user,%0A     
@@ -904,32 +904,36 @@
      user=user,%0A
+    
                 
@@ -954,32 +954,36 @@
 ferences=False,%0A
+    
             )%0A  
@@ -1068,16 +1068,113 @@
 = False%0A
+            logger.info('User %7B%7D was disabled'.format(user._id))%0A            if not dry_run:%0A    
         
@@ -1186,32 +1186,36 @@
 ails.send_mail(%0A
+    
                 
@@ -1237,16 +1237,20 @@
 ername,%0A
+    
         
@@ -1307,32 +1307,36 @@
                 
+    
 user=user,%0A     
@@ -1322,32 +1322,36 @@
      user=user,%0A
+    
                 
@@ -1391,32 +1391,36 @@
                 
+    
 can_change_prefe
@@ -1425,32 +1425,36 @@
 ferences=False,%0A
+    
             )%0A%0A 

put emails being sent in a dry run conditional