from django.db import models
from django.utils import timezone

import datetime
from azienda.models import Azienda,Sede
#from documento.models import *

class Utente(models.Model):
    codicefiscale = models.CharField(max_length=16,null=False)
    nome = models.CharField(max_length=128,null=False,default="")
    datanascita = models.DateField(null=True)
    luogonascita = models.CharField(max_length=256,null=False,default="")
    inserimento = models.DateField(null=False,default=timezone.now)
    mail = models.CharField(max_length=128,null=False,default="")
    azienda = models.ForeignKey(Azienda,on_delete=models.PROTECT,null=True)
    sede = models.ForeignKey(Sede,on_delete=models.PROTECT,null=True)
    daticompleti=models.BooleanField(null=False,default=0)
    pin = models.CharField(max_length=64,null=False,default="")
    encrypt=models.CharField(max_length=64,null=False,default="")
    uuid = models.CharField(max_length=32,null=False,default="")
    lostpin = models.BooleanField(null=False,default=False)
    cambiopassword = models.BooleanField(null=False,default=False)
    forzanuovapassword = models.BooleanField(null=False,default=False)
    otppassword = models.BooleanField(null=False,default=False)

    @property
    def documenti(self):
      return len(self.documento_set.all())
    
    class Meta:
      constraints = [ models.UniqueConstraint(fields=['nome','codicefiscale'], name="unique-utente") ]
      indexes = [ models.Index(fields=['codicefiscale','nome']) ]

      def __str__(self):
        return f"{self.id}: {self.nome}"

