import csv

from django.core.management import BaseCommand
import os
import requests
from django.conf import settings

from users.models import User, SchoolClass, Role, UserRole, SchoolClassMember


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('csv_file')
        parser.add_argument('--s3', dest='s3', action='store_const', default=False, const=True, help='the file is on a remote server and publicly accessible like e.g. an S3 resource')

    def import_users(self, file_handler):
        reader = csv.DictReader(file_handler)
        for row in reader:
            email = row['Email'].lower().strip()
            if email == '':
                self.stdout.write('No e-mail set, skipping')
                continue
            school_class_names = [c.strip() for c in row['Klassen'].split(',')]
            first_name = row['Vorname'].strip()
            last_name = row['Nachname'].strip()

            self.stdout.write("Creating user {} {}, {}".format(first_name, last_name, email))

            user = User.objects.create_user_with_random_password(first_name, last_name, email)

            if row['Rolle'] == 'Lehrer':
                self.stdout.write("Assigning teacher role")
                teacher = Role.objects.get(key='teacher')
                UserRole.objects.get_or_create(user=user, role=teacher)
            else:
                self.stdout.write("Assigning student role")
                student = Role.objects.get(key='teacher')
                UserRole.objects.get_or_create(user=user, role=student)

            self.stdout.write("Adding to class(es) {}".format(', '.join(school_class_names)))
            for school_class_name in school_class_names:
                school, _ = SchoolClass.objects.get_or_create(name=school_class_name)
                SchoolClassMember.objects.create(
                    school_class=school,
                    user=user
                )

            self.stdout.write("")

    def handle(self, *args, **options):
        self.stdout.write('Importing from {}!'.format(options['csv_file']))
        is_s3 = options['s3']
        csv_file = options['csv_file']
        try:
            if not is_s3:
                dir_path = settings.BASE_DIR
                rel_path = csv_file
                abs_path = os.path.join(dir_path, rel_path)

                with open(abs_path) as f:
                    self.import_users(f)
            else:
                with requests.Session() as s:
                    download = s.get(csv_file)
                    decoded_content = download.content.decode('utf-8')
                    self.import_users(decoded_content.splitlines())

        except Exception as e:
            self.stdout.write(e)
