from django.conf import settings
from django.db import models

from .user import CustomUser


class SousVivier(models.Model):
    """
    Modèle des sous-viviers de militaires et de postes
    """

    class Cols():
        """ Constantes pour les noms de colonnes """

        PK                = 'sv_id'
        LIBELLE           = 'sv_libelle'
        DOMAINE           = 'sv_dom'
        FILIERE           = 'sv_fil'
        CATEGORIE         = 'sv_cat'
        # relations many-to-many
        M2M_GESTIONNAIRES = 'gestionnaires'
        M2M_POSTES        = 'poste'

    sv_id = models.CharField(max_length=100, primary_key=True)
    sv_libelle = models.CharField(max_length=100)
    gestionnaires = models.ManyToManyField(CustomUser, related_name=CustomUser.Cols.M2M_SOUS_VIVIERS, blank=True)
    sv_dom = models.CharField(max_length=100, null=True, blank=True)
    sv_fil = models.CharField(max_length=100, null=True, blank=True)
    sv_cat = models.CharField(max_length=100, null=True, blank=True)

    def as_dict(self):
        mails = []
        for i in self.gestionnaires.all() :
            mails.append(i.email)
        return {
            "sv_id": self.sv_id,
            "sv_libelle": self.sv_libelle,
            "gestionnaires": mails,
        }
