import hashlib
import random
from django.contrib.auth.models import User, AbstractUser
from django.contrib.postgres.fields.array import ArrayField
from django.db import models
from django.utils.translation import gettext as _
from tasks.constants import IntroPopupType


class Apprentice(User):
    USERNAME_FIELD = 'email'

    activation_key = models.CharField(blank=True, max_length=40, verbose_name=_('klucz aktywacyjny'))
    intro_videos = ArrayField(models.SmallIntegerField(choices=IntroPopupType.choices, blank=True, null=True),
                              default=list, blank=True, verbose_name=_('obejrzane filmy intro'))

    class Meta:
        verbose_name = _('uczeń')
        verbose_name_plural = _('uczniowie')

    def generate_activation_key(self):
        salt = hashlib.sha1(str(random.random()).encode('utf-8')).hexdigest()[:5]
        self.activation_key = hashlib.sha1((salt + self.email).encode('utf-8')).hexdigest()

    def save(self, *args, **kwargs):
        if not self.pk:
            self.is_active = False
            self.generate_activation_key()
        super(Apprentice, self).save(args, kwargs)

    def __str__(self):
        return u'Apprentice - {}'.format(self.username, )


class Mentor(User):
    class Meta:
        verbose_name = _('mentor')
        verbose_name_plural = _('mentorzy')

    def __str__(self):
        return u'Mentor - {}'.format(self.username, )
