from django.contrib.auth.models import User, AbstractUser
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _

from access.customusermanager import CustomUserManager
from access.encryption import decrypt, get_module_validity, get_mac, encrypt, string_to_bytes, empty_licence
# Create your models here.



class KyeUser(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email


# The User and the profile (1 Manager) Classes need to be fed with an initial Fixture
class Profile(models.Model):
    ROLE_CHOICES = (
        ('Manager', 'Manager'),
        ('Vendor', 'Vendor'),
    )
    THEME_CHOICES = (
        ('default', 'Light'),
        ('cosmic', 'Cosmic'),
        ('dark', 'Dark'),
        ('corporate', 'Corporate'),
    )
    user = models.OneToOneField(KyeUser, on_delete=models.CASCADE, related_name="profile")
    role = models.CharField(choices=ROLE_CHOICES, max_length=10, null=True, blank=True)
    theme = models.CharField(choices=THEME_CHOICES, max_length=10, null=True, blank=True)

    def __str__(self):
        return "{} as a {}".format(self.user.username, self.role)


# The store Class need to be fed with an initial Fixture
class Store(models.Model):
    name = models.CharField(max_length=50, null=False, blank=False)
    address = models.CharField(max_length=300, null=False, blank=False)
    code = models.CharField(max_length=100, null=False, blank=False)

    def __str__(self):
        return self.name


# By Default the Licence must be an empty row (only 1 row)
class Licence(models.Model):
    # Generated by us
    encryption_key = models.CharField(max_length=500, null=True, blank=True, default='')

    # mac encrypted by encryption_key
    basic_key = models.CharField(max_length=50, null=True, blank=True, default='')

    # custmng : Customers Management
    custmng = models.CharField(max_length=200, null=True, blank=True, default='')

    # prvdmng : Providers Management
    prvdmng = models.CharField(max_length=200, null=True, blank=True, default='')

    # prodmng : Products Management
    prodmng = models.CharField(max_length=200, null=True, blank=True, default='')

    # mantmng : Maintenance Management
    mantmng = models.CharField(max_length=200, null=True, blank=True, default='')

    # mulstor : MultiStore
    mulstor = models.CharField(max_length=200, null=True, blank=True, default='')

    # muluser : MultiUser
    muluser = models.CharField(max_length=200, null=True, blank=True, default='')

    # statrpt : Statististics and Reports
    statrpt = models.CharField(max_length=200, null=True, blank=True, default='')

    def get_licences(self):
        licence = empty_licence()
        if self.encryption_key == '':
            return license
        else:
            key = string_to_bytes(self.encryption_key)
            if decrypt(string_to_bytes(self.basic_key), key) == get_mac():
                licence["valid"] = True
                licence["custmng"] = get_module_validity("custmng", decrypt(string_to_bytes(self.custmng), key))
                licence["prvdmng"] = get_module_validity("prvdmng", decrypt(string_to_bytes(self.prvdmng), key))
                licence["prodmng"] = get_module_validity("prodmng", decrypt(string_to_bytes(self.prodmng), key))
                licence["mantmng"] = get_module_validity("mantmng", decrypt(string_to_bytes(self.mantmng), key))
                licence["mulstor"] = get_module_validity("mulstor", decrypt(string_to_bytes(self.mulstor), key))
                licence["muluser"] = get_module_validity("muluser", decrypt(string_to_bytes(self.muluser), key))
                licence["statrpt"] = get_module_validity("statrpt", decrypt(string_to_bytes(self.statrpt), key))
            return licence




