from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.core.validators import EmailValidator

class Profile(AbstractUser):
    email = models.EmailField(max_length=255, unique=True, default="", validators=[EmailValidator()])

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ('password', )
    roleIndexLoginDefault = models.IntegerField(default=0)
    code = models.CharField(max_length=200, default="", blank=True)
    datePeremption = models.CharField(max_length=200, default="", blank=True)

    def __str__(self):
        return self.email

class ProfileRole(models.Model):
    class RoleType(models.IntegerChoices):
        PROFIL_UNDEFINED = -1, _('NON DEFINI')
        PROFIL_ECOLE = 0, _('ECOLE')
        PROFIL_ADMIN = 1, _('ADMIN')
        PROFIL_PARENT = 2, _('PARENT')

    profile = models.ForeignKey('Profile', on_delete=models.CASCADE, related_name='roles')
    role_type = models.IntegerField(choices=RoleType.choices, default=RoleType.PROFIL_UNDEFINED)
    establishment = models.ForeignKey('Establishment.Establishment', on_delete=models.CASCADE, related_name='profile_roles')
    is_active = models.BooleanField(default=False, blank=True)
    updated_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.profile.email} - {self.get_role_type_display()}"

class Directeur(models.Model):
    profile_role = models.OneToOneField("ProfileRole", on_delete=models.CASCADE, related_name='directeur_profile')
    last_name = models.CharField(max_length=100)
    first_name = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.first_name} {self.last_name} ({self.profile_role.profile.email})"