from datetime import timedelta

from django.utils.timezone import now

from core.factories import UserFactory
from users.factories import SchoolClassFactory, LicenseFactory
from users.licenses import MYSKILLBOX_LICENSES
from users.models import Role, UserRole, DEFAULT_SCHOOL_ID


def create_student(**kwargs):
    try:
        student_role = Role.objects.get_default_student_role()
    except Role.DoesNotExist:
        Role.objects.create_default_roles()
        student_role = Role.objects.get_default_student_role()
    user = UserFactory(
        **kwargs
    )
    UserRole.objects.create(user=user, role=student_role)
    return user


def create_users(data=None):
    Role.objects.create_default_roles()

    teacher_role = Role.objects.get_default_teacher_role()

    if data is None:
        #make an admin account
        UserFactory(username='admin', is_superuser=True, is_staff=True)

        teacher = UserFactory(username='teacher')
        UserRole.objects.create(user=teacher, role=teacher_role)

        students = []
        for i in range(1, 7):
            student = create_student(username='student{}'.format(i), onboarding_visited=True)
            students.append(student)

        SchoolClassFactory(
            users=[teacher] + students,
            name='skillbox',
        )
        teacher2 = UserFactory(username='teacher2')
        UserRole.objects.create(user=teacher2, role=teacher_role)

        student_second_class = create_student(username='student_second_class', onboarding_visited=True)
        SchoolClassFactory(
            users=[teacher2, student_second_class],
            name='second_class'
        )

    else:
        in_a_week = now() + timedelta(days=7)
        hep_id = 1

        for school_class in data:
            try:
                first, last, avatar_url = school_class.get('teacher')
            except ValueError:
                first, last = school_class.get('teacher')
                avatar_url = ''
            teacher = UserFactory(
                username='{}.{}'.format(first, last).lower(),
                first_name=first,
                last_name=last,
                email='{}.{}@skillbox.example'.format(first, last).lower(),
                onboarding_visited=True,
                license_expiry_date=in_a_week,
                hep_id=hep_id,
                avatar_url=avatar_url
            )
            UserRole.objects.create(user=teacher, role=teacher_role)

            students = []

            for student_data in school_class.get('students'):
                try:
                    first, last, avatar_url = student_data
                except ValueError:
                    first, last = student_data
                    avatar_url = ''
                hep_id += 1
                student = create_student(
                    username='{}.{}'.format(first, last).lower(),
                    first_name=first,
                    last_name=last,
                    email='{}.{}@skillbox.example'.format(first, last).lower(),
                    onboarding_visited=True,
                    license_expiry_date=in_a_week,
                    hep_id=hep_id,
                    avatar_url=avatar_url
                )

                students.append(student)

            SchoolClassFactory(
                users=students + [teacher],
                name=school_class.get('class'),
                code=school_class.get('code', '')
            )
