from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from .manger import UserManger
from django.contrib import admin


class Role(models.Model):
    libelle = models.CharField('Libelle', max_length=255, null=True, blank=True)
    description = models.CharField('Description', max_length=255, null=True, blank=True)

    def __str__(self):
        return self.libelle


class User(AbstractBaseUser, PermissionsMixin):
    avatar = models.ImageField('Avatar', null=True, blank=True, upload_to="images/avatars/")
    # nce1 = models.CharField('Nce', max_length=255, blank=True, null=True, unique=True)
    nom = models.CharField('Nom', max_length=255, null=False, blank=False)
    prenoms = models.CharField('Prenoms', max_length=255, null=False, blank=False)
    sexe = models.CharField('Sexe', max_length=255, null=False, blank=False, choices=(("M", "M"), ("F", "F")))
    datenaiss = models.DateField('Date de naissance', max_length=255, null=True, blank=True)
    lieunaiss = models.CharField('Lieu de naissance', max_length=255, null=True, blank=True)
    phone = models.CharField('Phone', max_length=255, null=True, blank=True, unique=True)
    email = models.EmailField('Email', max_length=255, null=False, blank=False, unique=True)
    adresse = models.CharField('Adresse', max_length=255, null=True, blank=True)

    civilite = models.CharField('Civilité', max_length=255, null=True, blank=True,
                                choices=(("M", "M"), ("Mme", "Mme"), ('Mlle', 'Mlle')))
    profession = models.CharField('Profession', max_length=255, null=True, blank=True,
                                  choices=(("Etudiant", "Etudiant"), ("Salarié", "Salarié"), ('Autres', 'Autres')))
    autre_profession = models.CharField('Autres profession', max_length=255, null=True, blank=True)

    nationalite = models.CharField('Nationalité', max_length=255, null=True, blank=True)
    groupe_sanguin = models.CharField('Groupe sanguin', max_length=255, null=True, blank=True)
    handicap = models.CharField('Handicap', max_length=255, null=True, blank=True)
    nb_enfant = models.IntegerField("Nombre d'enfant", null=True, blank=True, default=0)
    sit_matri = models.CharField('Situation matrimoniale', max_length=255, null=True, blank=True,
                                 choices=(("Célibataire", "Célibataire"), ("Marié(e)", "Marié(e)")))

    role = models.OneToOneField(Role, null=True, on_delete=models.SET_NULL, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)

    objects = UserManger()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['nom', 'prenoms']

    def __str__(self):
        return f"{self.nom} {self.prenoms}"

    def get_full_name(self):
        return f"{self.nom} {self.prenoms}"


class Title(models.Model):
    libelle = models.CharField('Libelle', max_length=255)

    class Meta:
        abstract = True

    def __str__(self) -> str:
        return self.libelle


class Annee(Title):
    status = models.CharField('Stauts', max_length=255)


class Domaine(Title):
    pass


class Specialite(Title):
    pass


class Mention(Title):
    pass


class Ufr(Title):
    code = models.CharField(max_length=10, null=True, blank=True)
    domaine = models.ForeignKey(Domaine, related_name="ufrs", on_delete=models.CASCADE, blank=True)


class Niveau(Title):
    pass


class Parcours(Title):
    pass


class Semestre(Title):
    niveau = models.ForeignKey(Niveau, related_name="semestre", null=True, default="1", on_delete=models.CASCADE)
    pass


class Session(Title):
    pass


class Salle(Title):
    pass


class Grade(Title):
    pass


MENTION = (
    ('PASSABLE', 'PASSABLE'),
    ('ASSEZ-BIEN', 'ASSEZ-BIEN'),
    ('BIEN', 'BIEN'),
    ('TRES-BIEN', 'TRES-BIEN'),
)

SERIES = (
    ('A1', 'A1'),
    ('A2', 'A2'),
    ('B', 'B'),
    ('C', 'C'),
    ('D', 'D'),
    ('E', 'E'),
    ('F1', 'F1'),
    ('F2', 'F2'),
    ('F3', 'F3'),
    ('F4', 'F4'),
    ('G1', 'G1'),
    ('G2', 'G2'),
    ('G3', 'G3'),

)

AGREE = (
    ('Oui', 'Oui'),
    ('Non', 'Non')
)


class Etudiant(User):
    nce = models.CharField('Nce', max_length=255, blank=True, null=True, unique=True)
    ancien_nce = models.CharField('Ancien Nce', max_length=255, blank=True, null=True, unique=True)
    ufr = models.ForeignKey(Ufr, related_name="etudiant", null=True, on_delete=models.CASCADE)

    boursier = models.CharField('Boursier', max_length=255, null=True, blank=True, choices=AGREE)
    salarie = models.CharField('Salarie', max_length=255, null=True, blank=True, choices=AGREE)
    quartier = models.CharField('Quartier de résidence', max_length=255, null=True, blank=True)

    # INFOS BAC
    matricule = models.CharField('Matricule', max_length=255, null=True, blank=True)
    annee_bac = models.IntegerField("Année d'obtension du BAC", null=True, blank=True)
    numero_bac = models.CharField('Numero du BAC', max_length=255, null=True, blank=True)
    serie_bac = models.CharField('Serie du BAC', max_length=255, null=True, blank=True,
                                 choices=SERIES)
    mention_bac = models.CharField('Serie du BAC', max_length=255, null=True, blank=True, choices=MENTION)
    centre_compo_bac = models.CharField("Centre d'obtension du BAC", max_length=255, null=True, blank=True)
    etablissement_bac = models.CharField('Etablissement', max_length=255, null=True, blank=True)
    type_etablissement = models.CharField("Type de l'établissement", max_length=255, null=True, blank=True,
                                          choices=(('PUBLIC', 'PUBLIC'), ('PRIVE', 'PRIVE')))
    #### INFOS TUTEUR
    nom_pere = models.CharField('Nom et prénoms du père', max_length=255, null=True, blank=True)
    nom_mere = models.CharField('Nom et prénoms de la mère', max_length=255, null=True, blank=True)
    contact_pere = models.CharField('Contact du père', max_length=255, null=True, blank=True)
    contact_mere = models.CharField('Contact de la mère', max_length=255, null=True, blank=True)
    nom_tuteur = models.CharField("Nom du tuteur", max_length=255, null=True, blank=True)
    contact_tuteur = models.CharField("Contact du tuteur", max_length=255, null=True, blank=True)
    nom_urgence = models.CharField("Nom du contact d'urgence", max_length=255, null=True, blank=True)
    contact_urgence = models.CharField("Contact en cas d'urgence", max_length=255, null=True, blank=True)
    habitation_contact_urgence = models.CharField("Lieu d'habitation du contact", max_length=255, null=True, blank=True)

    matricule_etat = models.CharField("Matricule d'Etat", max_length=255, null=True)
    statut = models.CharField("Statut", max_length=255, choices=(('AFF', 'AFF'), ('PPP', 'PPP')), null=True)
    domicile = models.CharField("Domicile", max_length=255, null=True)
    Numero_extrait = models.CharField("Numéro de l'Extrait", max_length=255, null=True)

    adresse_post = models.CharField('Adresse postale', max_length=255, null=True, blank=True)

    actif = models.BooleanField(default=True)
    process = models.BooleanField(default=False)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)
    USERNAME_FIELD = 'nce'


class Enseignant(User):
    grade = models.ForeignKey(Grade, related_name="enseignants", null=True, on_delete=models.CASCADE)
    ufr = models.ForeignKey(Ufr, related_name="enseignants", null=True, on_delete=models.CASCADE)
    ufrs = models.ManyToManyField(Ufr)
    profile = models.BooleanField(default=False, null=True)
    specialite = models.CharField('specialite', max_length=255, null=True, blank=False)
    statut = models.CharField('Statut', max_length=255, null=True, blank=False,
                              choices=(("Permanent", "Permanent"), ("Vacataire", "Vacataire")))
    regime = models.CharField('Régime', max_length=255, null=True, blank=False, default='Aucun',
                              choices=(
                                  ("Aucun", "Aucun"), ("Fonctionnaire", "Fonctionnaire"),
                                  ("Contractuel", "Contractuel")))
    is_ufr_dg = models.BooleanField(default=False, null=True)
    is_respo = models.BooleanField(default=False, null=True)
    is_ue = models.BooleanField(default=False, null=True)
    is_pr = models.BooleanField(default=False, null=True)
    is_vp = models.BooleanField(default=False, null=True)
    add_programme = models.BooleanField(default=False, null=True)
    interet = models.CharField("Centre d'intérêt ", max_length=255, null=True)


class GradeMentionUfr(Title):
    grade = models.ForeignKey(Grade, related_name="gradeMentionUfrs", on_delete=models.CASCADE)
    mention = models.ForeignKey(Mention, related_name="gradeMentionUfrs", on_delete=models.CASCADE)
    ufr = models.ForeignKey(Ufr, related_name="gradeMentionUfrs", on_delete=models.CASCADE)


class Ue(Title):
    code = models.CharField('Code', max_length=255)
    ufr = models.ForeignKey(Ufr, related_name="ues", null=True, on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, related_name="ues", null=True, on_delete=models.CASCADE)
    semestre = models.ForeignKey(Semestre, related_name="ues", null=True, on_delete=models.CASCADE)
    active = models.BooleanField(default=True)

    # used = models.ManyToManyField(Annee, related_name="ues", blank=True)

    def __str__(self):
        return str(self.libelle) + ' | ' + str(self.ufr.code) + ' | ' + str(self.niveau) + ' | ' + str(self.semestre)


class Ecue(Title):
    code = models.CharField('Code', max_length=255)
    credit = models.IntegerField('Credit')
    cm = models.IntegerField('CM', blank=True, null=True, default=0)
    td = models.IntegerField('TD', blank=True, null=True, default=0)
    tp = models.IntegerField('TP', blank=True, null=True, default=0)
    aem = models.IntegerField('AEM', blank=True, null=True, default=0)
    ue = models.ForeignKey(Ue, related_name="ecues", null=True, on_delete=models.CASCADE)
    active = models.BooleanField(default=True)
    # used = models.ManyToManyField(Annee, blank=True)


class InactiveUe(models.Model):
    annee = models.ForeignKey(Annee, related_name="ie", null=True, on_delete=models.CASCADE)
    ue = models.ForeignKey(Ue, related_name="ie", null=True, on_delete=models.CASCADE)
    created = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now=True)

    def __str__(self):
        return str(self.annee) + " ::" + str(self.ue.code) + " - " + str(self.ue.libelle) + ")"


class TypeEvaluation(Title):
    code = models.CharField('Code', max_length=10, null=True, blank=True)
    pourcentage = models.FloatField('Pourcentage', max_length=10, null=True, blank=True)


class Evaluation(Title):
    typeEvaluation = models.ForeignKey(TypeEvaluation, related_name="evaluations", on_delete=models.CASCADE)
    ecue = models.ForeignKey(Ecue, related_name="evaluations", on_delete=models.CASCADE)
    session = models.ForeignKey(Session, related_name="evaluations", on_delete=models.CASCADE, null=True, blank=True)
    annee = models.ForeignKey(Annee, related_name="evaluations", on_delete=models.CASCADE)
    date = models.DateTimeField('Date', auto_now=True)
    # Pour savoir quel enseignant en créer l'évaluation
    enseignant = models.ForeignKey(Enseignant, related_name="evaluations", on_delete=models.CASCADE, null=True,
                                   blank=True)
    is_noted = models.BooleanField(default=False, null=True, blank=True)


class AnonymatValidation(models.Model):
    isvalidate = models.BooleanField('Est Valide', max_length=255, default=False)
    ufr = models.ForeignKey(Ufr, related_name="anonymatValidations", on_delete=models.CASCADE)
    evaluation = models.ForeignKey(Evaluation, related_name="anonymatValidations", on_delete=models.CASCADE)


class Reclamation(Title):
    etudiant = models.ForeignKey(Etudiant, related_name="reclamations", on_delete=models.CASCADE)
    ue = models.ForeignKey(Ue, related_name="reclamations", on_delete=models.CASCADE)
    motif = models.CharField('Motif', max_length=255)
    date = models.DateField('Date', auto_now=True)


class Classe(models.Model):
    status = models.CharField('Status', null=True, max_length=255)
    annee = models.ForeignKey(Annee, related_name="classes", null=True, on_delete=models.CASCADE)
    etudiants = models.ManyToManyField(Etudiant, related_name="classes")
    ufr = models.ForeignKey(Ufr, related_name="classes", null=True, on_delete=models.CASCADE)
    parcours = models.ForeignKey(Parcours, related_name="classes", null=True, on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, related_name="classes", null=True, on_delete=models.CASCADE)


class MoyenneUe(models.Model):
    annee = models.ForeignKey(Annee, related_name="moyennes", on_delete=models.CASCADE)
    session = models.ForeignKey(Session, related_name="moyennes", on_delete=models.CASCADE)
    etudiant = models.ForeignKey(Etudiant, related_name="moyennes", on_delete=models.CASCADE)
    ue = models.ForeignKey(Ue, related_name="moyennes", on_delete=models.CASCADE)
    # ecue_1 = models.FloatField('Moyenne ecue_1')
    # ecue_2 = models.FloatField('Moyenne ecue_2')
    moyenne_reelle = models.FloatField('Moyenne reelle', null=True, blank=True)
    moyenne_repechee = models.FloatField('Moyenne repechee', null=True, blank=True)
    valide = models.BooleanField('Valide', default=False)


class MoyenneEcue(models.Model):
    ecue = models.ForeignKey(Ecue, related_name="moyennes", on_delete=models.CASCADE)
    moyenneUe = models.ForeignKey(MoyenneUe, related_name="moyenneUes", on_delete=models.CASCADE, null=True, blank=True,
                                  default=None)
    note = models.FloatField('Moyenne reelle', default=None, blank=True, null=True)
    note_repechee = models.FloatField('Moyenne repechee', default=None, blank=True, null=True)


class ParcoursSemestre(Title):
    parcours = models.ForeignKey(Parcours, related_name="parcoursSemestres", on_delete=models.CASCADE)
    semestre = models.ForeignKey(Semestre, related_name="parcoursSemestres", on_delete=models.CASCADE)
    gradeMentionUfr = models.ForeignKey(GradeMentionUfr, related_name="parcoursSemestres", on_delete=models.CASCADE)


class ParcoursSemestreUe(models.Model):
    annee = models.ForeignKey(Annee, related_name="parcoursSemestreUes", on_delete=models.CASCADE)
    ue = models.ForeignKey(Ue, related_name="parcoursSemestreUes", on_delete=models.CASCADE)
    parcoursSemestre = models.ForeignKey(ParcoursSemestre, related_name="parcoursSemestreUes", on_delete=models.CASCADE)

    def __str__(self) -> str:
        return f"{self.annee.libelle} : {self.ue.libelle} : {self.parcoursSemestre}"


class EtudiantUe(models.Model):
    etudiant = models.ForeignKey(Etudiant, related_name="etudiantUes", on_delete=models.CASCADE)
    parcoursSemestreUe = models.ForeignKey(ParcoursSemestreUe, related_name="etudiantUes", on_delete=models.CASCADE)


class EnseignantUe(models.Model):
    enseignant = models.ForeignKey(Enseignant, related_name="enseignantUes", on_delete=models.CASCADE)
    ecue = models.ForeignKey(Ecue, related_name="enseignantUes", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="enseignantUes", on_delete=models.CASCADE, blank=True, null=True)


class EnseignantUfr(models.Model):
    annee = models.ForeignKey(Annee, related_name="enseignantUfrs", on_delete=models.CASCADE, blank=True, null=True)
    enseignant = models.ForeignKey(Enseignant, related_name="enseignantUfrs", on_delete=models.CASCADE)
    ufr = models.ForeignKey(Ufr, related_name="enseignantUfrs", on_delete=models.CASCADE)

    def __str__(self):
        return str(self.annee) + '::' + str(self.enseignant) + '::' + str(self.ufr)


class ResponsableUe(models.Model):
    enseignantUe = models.ForeignKey(EnseignantUe, related_name="responsableUes", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="responsableUes", on_delete=models.CASCADE)


class Anonymat(models.Model):
    code = models.CharField('Code', max_length=255)
    annee = models.ForeignKey(Annee, related_name="anonymats", on_delete=models.CASCADE)
    ecue = models.ForeignKey(Ecue, related_name="anonymats", on_delete=models.CASCADE)
    etudiant = models.ForeignKey(Etudiant, related_name="anonymats", on_delete=models.CASCADE)


class Note(models.Model):
    note = models.FloatField('Note', max_length=255, blank=True, null=True, default=0)
    etudiant = models.ForeignKey(Etudiant, related_name="notes", on_delete=models.CASCADE)
    evaluation = models.ForeignKey(Evaluation, related_name="notes", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="notes", on_delete=models.CASCADE, null=True, blank=True)
    ecue = models.ForeignKey(Ecue, related_name="notes", on_delete=models.CASCADE, null=True, blank=True)
    date = models.DateField("Date", max_length=255, auto_now=True)
    isBlocked = models.BooleanField("Modifiable", default=False)

    def __str__(self):
        return str(self.etudiant) + " (" + str(self.evaluation) + ": " + str(self.note) + ")"


class Seance(Title):
    ecue = models.ForeignKey(Ecue, related_name="seances", on_delete=models.CASCADE)
    enseignants = models.ManyToManyField(Enseignant, related_name="seances")
    salle = models.ForeignKey(Salle, related_name="seances", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="seances", on_delete=models.CASCADE)
    date = models.DateField("Date", auto_now=True)
    duree = models.TimeField("Duree", auto_now=True)
    heureDebut = models.DateTimeField("Heure de debut", auto_now=True)


TYPECOURS = (
    ('CM', 'CM'),
    ('TP', 'TP'),
    ('TD', 'TD')
)


class SeanceCours(models.Model):
    type_cours = models.CharField('Type du Cours', max_length=255, null=True, choices=TYPECOURS)
    ecue = models.ForeignKey(Ecue, related_name="seancecours", on_delete=models.CASCADE)
    enseignant = models.ForeignKey(Enseignant, related_name="seancecours", on_delete=models.CASCADE)
    salle = models.ForeignKey(Salle, related_name="seancecours", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="seancecours", on_delete=models.CASCADE)
    date = models.DateField("Date de début", null=True)
    debut = models.TimeField("Heure de début", null=True)
    fin = models.TimeField("Heure de fin", null=True)
    debut_delegue = models.TimeField("Heure de début du délégué", null=True)
    fin_delegue = models.TimeField("Heure de fin du délégué", null=True)
    # enseignant_valid = models.BooleanField('Validation Enseignant',default=False)
    etudiant_valid = models.BooleanField('Validation Etudiant', default=False)
    admin_valid = models.BooleanField('Validation Admin', default=False)
    paye = models.BooleanField('Payée', default=False)
    contenu = models.TextField(null=True)

    # duree = models.IntegerField("Duree (en min)", null=True)

    def __str__(self):
        return str(self.date) + '::' + str(self.debut) + '-' + str(self.fin) + '::ECUE:' + str(self.ecue.code)


class NiveauEtudiant(models.Model):
    etudiant = models.ForeignKey(Etudiant, related_name="niveauetudiant", on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, related_name="niveauetudiant", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="niveauetudiant", on_delete=models.CASCADE)

    def __str__(self):
        return str(self.annee) + "," + str(self.niveau) + ", " + str(self.etudiant)


JOURS = (
    ('1', 'Lundi'),
    ('2', 'Mardi'),
    ('3', 'Mercredi'),
    ('4', 'Jeudi'),
    ('5', 'Vendredi'),
    ('6', 'Samedi'),
    ('7', 'Dimanche'),
)


class Jour(Title):
    pass

    def __str__(self):
        return self.libelle


class EmploiDuTemps(models.Model):
    ecue = models.ForeignKey(Ecue, related_name="emploidutemps", on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, related_name="emploidutemps", on_delete=models.CASCADE)
    enseignants = models.ForeignKey(Enseignant, related_name="emploidutemps", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="emploidutemps", on_delete=models.CASCADE)
    dateDebut = models.DateField("Date de Début", null=True)
    dateFin = models.DateField("Date de Fin", null=True)
    activated = models.BooleanField(default=True, null=True)
    ceated = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now=True)

    def __str__(self):
        return str(self.annee) + "," + str(self.niveau) + ", " + str(self.ecue)


class Validite(models.Model):
    dateDebut = models.DateField("Date de Début", null=True)
    dateFin = models.DateField("Date de Fin", null=True)
    activated = models.BooleanField(default=False, null=True)
    done = models.BooleanField(default=False, null=True)

    def __str__(self):
        return 'du ' + str(self.dateDebut) + ' au ' + str(self.dateFin)


TYPE = (
    ('COURS', 'COURS'),
    ('DEVOIR', 'DEVOIR'),
)

MODE = (
    ('Présentiel', 'Présentiel'),
    ('En ligne', 'En ligne'),
)


class EmploiDuTemps2(models.Model):
    ecue = models.ForeignKey(Ecue, related_name="emploidutemps2", on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, related_name="emploidutemps2", on_delete=models.CASCADE)
    enseignants = models.ForeignKey(Enseignant, related_name="emploidutemps2", on_delete=models.CASCADE, blank=True)
    validite = models.ForeignKey(Validite, related_name="emploidutemps2", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="emploidutemps2", on_delete=models.CASCADE)
    lundi = models.CharField("Lundi", null=True, max_length=20)
    mardi = models.CharField("Mardi", null=True, max_length=20)
    mercredi = models.CharField("mercredi", null=True, max_length=20)
    jeudi = models.CharField("Jeudi", null=True, max_length=20)
    vendredi = models.CharField("vendredi", null=True, max_length=20)
    samedi = models.CharField("Samedi", null=True, max_length=20)

    type_lun = models.CharField("Type Lundi", null=True, max_length=20, choices=TYPE)
    type_mar = models.CharField("Type Mardi", null=True, max_length=20, choices=TYPE)
    type_mer = models.CharField("Type Mercredi", null=True, max_length=20, choices=TYPE)
    type_jeu = models.CharField("Type Jeudi", null=True, max_length=20, choices=TYPE)
    type_ven = models.CharField("Type Vendredi", null=True, max_length=20, choices=TYPE)
    type_sam = models.CharField("Type Samedi", null=True, max_length=20, choices=TYPE)
    salle = models.ForeignKey(Salle, related_name="emploidutemps2", on_delete=models.CASCADE)

    salle_lun = models.ForeignKey(Salle, related_name="sal_lun", null=True, blank=True, on_delete=models.CASCADE)
    salle_mar = models.ForeignKey(Salle, related_name="sal_mar", null=True, blank=True, on_delete=models.CASCADE)
    salle_mer = models.ForeignKey(Salle, related_name="sal_mer", null=True, blank=True, on_delete=models.CASCADE)
    salle_jeu = models.ForeignKey(Salle, related_name="sal_jeu", null=True, blank=True, on_delete=models.CASCADE)
    salle_ven = models.ForeignKey(Salle, related_name="sal_ven", null=True, blank=True, on_delete=models.CASCADE)
    salle_sam = models.ForeignKey(Salle, related_name="sal_sam", null=True, blank=True, on_delete=models.CASCADE)

    salle2_lun = models.ManyToManyField(Salle, related_name="sal2_lun", blank=True)
    salle2_mar = models.ManyToManyField(Salle, related_name="sal2_mar", blank=True)
    salle2_mer = models.ManyToManyField(Salle, related_name="sal2_mer", blank=True)
    salle2_jeu = models.ManyToManyField(Salle, related_name="sal2_jeu", blank=True)
    salle2_ven = models.ManyToManyField(Salle, related_name="sal2_ven", blank=True)
    salle2_sam = models.ManyToManyField(Salle, related_name="sal2_sam", blank=True)

    mode_lun = models.CharField("Mode Lundi", null=True, max_length=20, choices=MODE)
    mode_mar = models.CharField("Mode Mardi", null=True, max_length=20, choices=MODE)
    mode_mer = models.CharField("Mode Mercredi", null=True, max_length=20, choices=MODE)
    mode_jeu = models.CharField("Mode Jeudi", null=True, max_length=20, choices=MODE)
    mode_ven = models.CharField("Mode Vendredi", null=True, max_length=20, choices=MODE)
    mode_sam = models.CharField("Mode Samedi", null=True, max_length=20, choices=MODE)

    url_lun = models.CharField("Url Lundi", null=True, default='', max_length=255)
    url_mar = models.CharField("Url Mardi", null=True, default='', max_length=255)
    url_mer = models.CharField("Url Mercredi", null=True, default='', max_length=255)
    url_jeu = models.CharField("Url Jeudi", null=True, default='', max_length=255)
    url_ven = models.CharField("Url Vendredi", null=True, default='', max_length=255)
    url_sam = models.CharField("Url Samedi", null=True, default='', max_length=255)

    ceated = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now=True)
    activated = models.BooleanField(default=True, null=True)

    def __str__(self):
        return str(self.annee) + "," + str(self.niveau) + ", " + str(self.ecue)


class EdtJour(models.Model):
    programme = models.ForeignKey(EmploiDuTemps, related_name="edtjour", on_delete=models.CASCADE, null=True)
    jour = models.ForeignKey(Jour, related_name="edtjour", on_delete=models.CASCADE, null=True)
    salle = models.ForeignKey(Salle, related_name="edtjour", on_delete=models.CASCADE)
    heureDebut = models.CharField("Heure de debut", null=True, max_length=6)

    def __str__(self):
        return str(self.programme) + "," + str(self.jour)


TYPE_DEL = (
    ('Ecue', 'Ecue'),
    ('Ue', 'Ue')
)


class Deliberation(models.Model):
    annee = models.ForeignKey(Annee, related_name="deliberations", on_delete=models.CASCADE, null=True)
    ecue = models.ForeignKey(Ecue, related_name="deliberations", on_delete=models.CASCADE, default=None)
    session = models.ForeignKey(Session, related_name="deliberations", on_delete=models.CASCADE, default=None,
                                null=True)
    note = models.FloatField('Note', max_length=255, null=True)
    user = models.ForeignKey(Enseignant, related_name="deliberations", on_delete=models.CASCADE, default=None,
                             null=True)
    created = models.DateTimeField(auto_now_add=True, null=True, blank=True)

    def __str__(self):
        return str(self.annee) + ',' + str(self.session) + ',' + str(self.ecue) + ',' + str(self.note)


class DeliberationUe(models.Model):
    annee = models.ForeignKey(Annee, related_name="deliberationsUe", on_delete=models.CASCADE, null=True)
    ue = models.ForeignKey(Ue, related_name="deliberationsUe", on_delete=models.CASCADE, default=None)
    session = models.ForeignKey(Session, related_name="deliberationsUe", on_delete=models.CASCADE, default=None,
                                null=True)
    note = models.FloatField('Note', max_length=255, null=True)
    user = models.ForeignKey(Enseignant, related_name="deliberationsUe", on_delete=models.CASCADE, default=None,
                             null=True)
    created = models.DateTimeField(auto_now_add=True, null=True, blank=True)

    def __str__(self):
        return str(self.annee) + ',' + str(self.session) + ',' + str(self.ue) + ',' + str(self.note)


# CSV
class Csv(models.Model):
    file_name = models.FileField(upload_to='csv/%Y/%m/%d')
    uploaded = models.DateTimeField(auto_now_add=True)
    activated = models.BooleanField(default=False)


class CsvAdmin(admin.ModelAdmin):
    list_display = ('id', 'file_name', 'uploaded', 'activated')
    search_fields = ['file_name']
    list_per_page = 50


MOTIF_CHOICES = (
    ("Erreur sur l'identité", "Erreur sur l'identité"),
    ("Omission de note d'examens ou TD", "Omission de note d'examens ou TD"),
    ("Moyenne mal calculée", "Moyenne mal calculée"),
    ("Autres", "Autres"),
)


class Reclamations(models.Model):
    etudiant = models.ForeignKey(Etudiant, related_name="reclamationse", on_delete=models.CASCADE)
    ufr = models.ForeignKey(Ufr, related_name="reclamationse", on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, related_name="reclamationse", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="reclamationse", on_delete=models.CASCADE)
    type_discipline = models.CharField("Type Discipline", max_length=20,
                                       choices=(('UE', 'UE'), ('ECUE', 'ECUE'), ('Autres', 'Autres')), null=True,
                                       blank=True)
    code_discipline = models.CharField("Code Discipline", max_length=20, null=True, blank=True)
    motif = models.CharField("Motif", max_length=100, choices=MOTIF_CHOICES, null=True, blank=True)
    detail = models.TextField("Detail", null=True, blank=True)
    status = models.CharField("Status", max_length=20, choices=(
        ('Recevable', 'Recevable'), ('Non-recevable', 'Non-recevable'), ('En Attente', 'En Attente')),
                              default='En Attente')
    reception = models.BooleanField(default=False)
    archive = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.etudiant) + '::' + str(self.motif)


class SyllabusModel(models.Model):
    pubNivVise = models.TextField("Public et Niveau Visé", null=True, blank=True)
    description = models.TextField("Description", null=True, blank=True)
    duree = models.CharField("Durée", max_length=255, null=True, blank=True)
    credit = models.CharField("Crédit", max_length=255, null=True, blank=True)
    objectif = models.TextField("Objectifs", null=True, blank=True)
    Competence = models.TextField("Compétence", null=True, blank=True)
    prerequis = models.TextField("Pré-requis", null=True, blank=True)
    participant = models.TextField("Participants", null=True, blank=True)
    versionDate = models.TextField("No. version et Date", null=True, blank=True)
    bibliographie = models.TextField("Bibliographie", null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)


class SyllabusEcue(SyllabusModel):
    ecue = models.ForeignKey(Ecue, on_delete=models.CASCADE)

    def __str__(self):
        return "Syllabus_ECUE_" + str(self.ecue.code)


class CategorieUe(models.Model):
    libelle = models.CharField("Libelle", max_length=255, null=True, blank=True)

    def __str__(self):
        return str(self.libelle)


class SyllabusUe(SyllabusModel):
    ue = models.ForeignKey(Ecue, related_name="syllabusue", on_delete=models.CASCADE)
    categorie = models.ManyToManyField(CategorieUe, related_name="syllabusue", blank=True)

    def __str__(self):
        return "Syllabus_ECUE_" + str(self.ue.code)


class TypeDemandeActes(Title):
    cible = models.CharField("Cible", max_length=255,
                             choices=(('tout', 'tout'), ('ufr', 'ufr'), ('scolarité', 'scolarité')), null=True)


class DemandeActes(models.Model):
    etudiant = models.ForeignKey(Etudiant, related_name="demact", on_delete=models.CASCADE)
    ufr = models.ForeignKey(Ufr, related_name="demact", on_delete=models.CASCADE)
    annee = models.ForeignKey(Annee, related_name="demact", on_delete=models.CASCADE)
    type_demande = models.ForeignKey(TypeDemandeActes, related_name="demact", on_delete=models.CASCADE)
    reception = models.BooleanField(default=False)
    archive = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.etudiant) + '::' + str(self.type_demande)


class ReponseDemandeActes(models.Model):
    demande = models.ForeignKey(DemandeActes, on_delete=models.CASCADE, null=True)
    document = models.FileField(upload_to='documents/%Y', null=True)
    reception = models.BooleanField(default=False)
    archive = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.demande.etudiant) + '::' + str(self.demande.type_demande)


class TypeDocument(models.Model):
    libelle = models.CharField('Libellé', max_length=255, null=True, blank=True)
    description = models.CharField('Description', max_length=255, null=True, blank=True)

    def __str__(self):
        return self.libelle


class Document(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True, editable=False)
    typedoc = models.ForeignKey(TypeDocument, on_delete=models.CASCADE, null=True)
    etudiant = models.ForeignKey(Etudiant, on_delete=models.CASCADE, null=True)
    document = models.FileField(upload_to='documents/%Y')
    valid = models.BooleanField(default=False)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    # def save(self, *args, **kwargs):
    #    last_annee = Annee.objects.last()
    #    if last_annee:
    #        self.annee = last_annee
    #    else:
    #        self.annee = None
    #    super().save(*args, **kwargs)

    def __str__(self):
        return str(self.typedoc) + '_' + str(self.etudiant)


class PvArchive(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True)
    niveau = models.ForeignKey(Niveau, on_delete=models.CASCADE, null=True)
    ufr = models.ForeignKey(Ufr, on_delete=models.CASCADE, null=True)
    document = models.FileField(upload_to='pv/%Y')
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + '_' + str(self.ufr.code)


class Reinscription(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True)
    etudiant = models.ForeignKey(Etudiant, on_delete=models.CASCADE, null=True)
    niveau = models.ForeignKey(Niveau, on_delete=models.CASCADE, null=True)
    done = models.BooleanField(default=False)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + '_' + str(self.etudiant)


class Compensation(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True)
    etudiant = models.ForeignKey(Etudiant, on_delete=models.CASCADE, null=True)
    ue = models.ForeignKey(Ue, on_delete=models.CASCADE, null=True, blank=True)
    note = models.FloatField('Note', max_length=255, blank=True, null=True, default=None)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + '_' + str(self.etudiant) + '_' + str(self.ue.code) + '_' + str(self.note)


# +++++++++++++++++++++ PERSO MODEL +++++++++++++++++++++++++++++++

class PsTypeCours(Title):
    prerequis = models.CharField("Pré-requis", max_length=255, null=True)
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return str(self.enseignant) + '|' + str(self.libelle)


class PsCours(Title):
    titre = models.CharField("Titre", max_length=255, null=True)
    type_cours = models.ForeignKey(PsTypeCours, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return str(self.libelle)


class PsCours2(Title):
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE, null=True)
    niveau = models.CharField("Niveau", max_length=255, null=True, blank=True)
    annee = models.CharField("Annee", max_length=255, null=True, blank=True)
    etablissement = models.CharField("Etablissement", max_length=255, null=True, blank=True)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.libelle)


class PsLogiciel(Title):
    fonction = models.CharField("Fonction", max_length=255, null=True)
    description = models.TextField("Description", null=True)
    participant = models.CharField("Participants", max_length=255, null=True)
    url = models.CharField("URL", max_length=255, null=True)
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return str(self.enseignant) + '|' + str(self.libelle)


class PsTypePublication(Title):
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return str(self.enseignant) + '|' + str(self.libelle)


class PsTypePublication2(Title):
    pass

    def __str__(self):
        return str(self.libelle)


class PsPublication(models.Model):
    type_pub = models.ForeignKey(PsTypePublication, on_delete=models.CASCADE, null=True)
    typub = models.ForeignKey(PsTypePublication2, on_delete=models.CASCADE, null=True, blank=True)
    annee = models.IntegerField('Année de Publication', null=True)
    titre = models.CharField("Titre", max_length=255, null=True)
    reference = models.CharField("Référence", max_length=255, null=True)
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE, null=True)
    auteurs = models.CharField("Auteur(s)", max_length=255, null=True)
    volume = models.CharField("Volume", max_length=255, null=True)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.enseignant) + ' | ' + str(self.titre)


class PsGroupeRech(Title):
    institut = models.CharField("Titre", max_length=255, null=True)
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return str(self.enseignant) + '|' + str(self.libelle)


class PsPJoint(Title):
    cours = models.ForeignKey(PsCours, on_delete=models.CASCADE, null=True, blank=True)
    cours2 = models.ForeignKey(PsCours2, on_delete=models.CASCADE, null=True, blank=True)
    pub = models.ForeignKey(PsPublication, on_delete=models.CASCADE, null=True, blank=True)
    fichier = models.FileField(upload_to='documents/%Y')


class GroupTravail(Title):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE)
    type_cours = models.CharField('Type du Cours', max_length=255, null=True, choices=TYPECOURS)
    limite = models.IntegerField('Limite de place', null=True)
    ufr = models.ForeignKey(Ufr, on_delete=models.CASCADE)
    niveau = models.ForeignKey(Niveau, on_delete=models.CASCADE)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + '::' + str(self.libelle) + '|' + str(self.niveau.libelle) + '|' + str(self.ufr.code)


class GroupEtudiant(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE)
    groupe = models.ForeignKey(GroupTravail, on_delete=models.CASCADE)
    etudiant = models.ForeignKey(Etudiant, on_delete=models.CASCADE)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + '::' + str(self.groupe.libelle) + '|' + str(self.etudiant)


class GroupEnseignant(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE)
    groupe = models.ForeignKey(GroupTravail, on_delete=models.CASCADE)
    enseignant = models.ForeignKey(Enseignant, on_delete=models.CASCADE)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + '::' + str(self.groupe.libelle) + '|' + str(self.enseignant)


class FlashInfo(models.Model):
    titre = models.CharField("Titre", max_length=255, null=True)
    message = models.CharField("Message", max_length=255, null=True)
    ufr = models.CharField("Ufr", max_length=255, choices=(
        ('Tout', 'Tout'), ('ARHAI', 'ARHAI'), ('MPSI', 'MPSI'), ('LTHR', 'LTHR'), ('SDM', 'SDM')), null=True)
    cible = models.CharField("Cible", max_length=255,
                             choices=(('Tout', 'Tout'), ('Enseignant', 'Enseignant'), ('Etudiant', 'Etudiant')),
                             null=True)
    limite = models.DateField('Date limite', null=True)
    published = models.BooleanField(default=False)
    created = models.DateField(auto_now_add=True, null=True)

    def __str__(self):
        return str(self.titre)


tpd = (
    ('Sondage', 'Sondage'),
    ('Réclamation', 'Réclamation'),
)


class Diffusion(models.Model):
    titre = models.CharField("Titre", max_length=255,
                             choices=tpd)
    limite = models.DateField('Date limite', null=True)
    publier = models.BooleanField(default=False)

    def __str__(self):
        return str(self.titre) + ' | ' + str(self.publier)


class Sondage(models.Model):
    titre = models.CharField("Titre", max_length=255, null=True)
    url = models.CharField("Url", max_length=255, null=True)
    ufr = models.CharField("Ufr", max_length=255, choices=(
        ('Tout', 'Tout'), ('ARHAI', 'ARHAI'), ('MPSI', 'MPSI'), ('LTHR', 'LTHR'), ('SDM', 'SDM')), null=True)
    cible = models.CharField("Cible", max_length=255,
                             choices=(('Tout', 'Tout'), ('Enseignant', 'Enseignant'), ('Etudiant', 'Etudiant')),
                             null=True)
    limite = models.DateField('Date limite', null=True)
    published = models.BooleanField(default=True)
    created = models.DateField(auto_now_add=True, null=True)

    def __str__(self):
        return str(self.created) + ' | ' + str(self.titre)


class Direction(Title):
    code = models.CharField("Code", max_length=255, null=True)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.code) + ' | ' + str(self.libelle)


class Service(Title):
    code = models.CharField("Code", max_length=255, null=True)
    direction = models.ForeignKey(Direction, null=True, on_delete=models.CASCADE, blank=True)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.code) + ' | ' + str(self.libelle)


class Personnel(User):
    prise_de_fonction = models.DateField('Prise de fonction', max_length=255, null=True, blank=True)
    resposabilite = models.ForeignKey(Role, null=True, related_name="personnel", on_delete=models.SET_NULL, blank=True)
    regime = models.CharField('Régime', max_length=255, null=True, blank=False, default='Aucun',
                              choices=(
                                  ("Aucun", "Aucun"), ("Fonctionnaire", "Fonctionnaire"),
                                  ("Contractuel", "Contractuel")))
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)


class PersonnelService(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True, blank=True)
    personnel = models.ForeignKey(Personnel, on_delete=models.CASCADE, null=True, blank=True)
    service = models.ForeignKey(Service, on_delete=models.CASCADE, null=True, blank=True)
    prise_de_fonction = models.DateField('Prise de fonction', max_length=255, null=True, blank=True)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.service) + ' | ' + str(self.personnel) + ' | ' + str(self.prise_de_fonction)


class ResponsableService(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True, blank=True)
    responsable = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    service = models.ForeignKey(Service, on_delete=models.CASCADE, null=True, blank=True)
    prise_de_fonction = models.DateField('Prise de fonction', max_length=255, null=True, blank=True)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.service) + ' | ' + str(self.responsable) + ' | ' + str(self.prise_de_fonction)


class Presence(models.Model):
    annee = models.ForeignKey(Annee, on_delete=models.CASCADE, null=True, blank=True)
    etudiant = models.ForeignKey(Etudiant, on_delete=models.CASCADE, null=True, blank=True)
    ecue = models.ForeignKey(Ecue, on_delete=models.CASCADE, null=True, blank=True)
    date = models.DateField("Date", null=True)
    debut = models.TimeField("Heure de Début", null=True)
    fin = models.TimeField("Heure de Fin", null=True)
    nombre_heure = models.IntegerField("Nombre d'heure", default=0)
    created = models.DateField(auto_now_add=True, null=True)
    update = models.DateField(auto_now=True, null=True)

    def __str__(self):
        return str(self.annee) + ' | ' + str(self.etudiant) + ' | ' + str(self.ecue.code)

###################################################################


