import random
from datetime import timedelta

import factory

from users.models import SchoolClass, SchoolClassMember, License, Team
from django.utils.timezone import now


class_types = ['DA', 'KV', 'INF', 'EE']
class_suffix = ['A', 'B', 'C', 'D', 'E']


# TODO: refactor to use a trait synching the year with the infix year
# TODO: refactor to have non-overlapping user groups?


class SchoolClassFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = SchoolClass

    name = factory.Sequence(
        lambda n: '{}{}{}'.format(random.choice(class_types), '18', class_suffix[n % len(class_suffix)]))
    is_deleted = False

    @factory.post_generation
    def users(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for user in extracted:
                SchoolClassMember.objects.create(user=user, school_class=self, active=True)


class TeamFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Team

    name = factory.Faker('name')
    code = factory.Sequence(lambda n: "CODE{}".format(n))
    is_deleted = False


class LicenseFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = License

    expire_date = now() + timedelta(days=7)
    order_id = factory.Sequence(lambda n: n)
