from bulk_update_or_create import BulkUpdateOrCreateQuerySet
from django.db import models

from .commun import NiveauFonctionnelChoices, SpecifiqueChoices
from .competence import Competence
from .domaine import Domaine
from .filiere import Filiere
from .fonction import Fonction
from .formation_emploi import FormationEmploi
from .grade import Grade
from .sous_vivier import SousVivier
from .pam import PAM
class SuiviPrevisionnelMilitaire(models.TextChoices):
    """
    [Administre] choix pour le gestionnaire PCP
    """

    MOB_EXT = 'MOB_EXT', 'Mobilité externe'
    MOB_INT = 'MOB_INT', 'Mobilité interne',
    NRCT    = 'NRCT',    'NRCT',
    RDC     = 'RDC',     'RDC',
    LMT     = 'LMT',     'Limite de contrat',
    REC     = 'REC',     'Reconversion',
    REC_OE  = 'REC_OE',  'Recrutement OE',
    DFP     = 'DFP',     'Détachement FP',


class StatutFuturChoices(models.TextChoices):
    """
    [Administre] choix pour le statut futur
    """

    CLDM   = 'CLDM',   'CLDM'
    CP     = 'CP',     'CP'
    DRJI   = 'DRJI',   'DRJI'
    NRCT   = 'NRCT',   'NRCT'
    DET    = 'DET',    'DET'
    RECONV = 'RECONV', 'RECONV'


class StatutPamChoices(models.TextChoices):
    """
    [Administre] choix pour le statut PAM
    attributs supplémentaires :
    - calc_enabled : est-ce que ce statut permet le calcul ?
    - dec_enabled  : est-ce que ce statut permet de créer une décision ?
    """

    #             (valeur, calc_enabled, dec_enabled), libellé
    A_ETUDIER      = ('A_ETUDIER',      True,  True),  'A étudier'
    A_ETUDIER_REC  = ('A_ETUDIER_REC',  True,  True),  'A étudier REC'
    A_MAINTENIR    = ('A_MAINTENIR',    False, True),  'A maintenir'
    A_MUTER        = ('A_MUTER',        True,  True),  'A muter'
    A_TRAITER      = ('A_TRAITER',      False, False), 'A traiter'
    NON_DISPONIBLE = ('NON_DISPONIBLE', False, False), 'Non disponible'
    NON_ETUDIE     = ('NON_ETUDIE',     False, False), 'Non étudié'
    PARTANT        = ('PARTANT',        False, False), 'Partant'

    def __new__(cls, value):
        obj = str.__new__(cls, value[0])
        obj._value_ = value[0]
        obj.calc_enabled = value[1]
        obj.dec_enabled  = value[2]
        return obj


class Administre(models.Model):
    """
    Modèle des administrés
    """

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

        PK                         = 'a_id_sap'
        CATEGORIE                  = 'a_categorie'
        STATUT_CONCERTO            = 'a_statut_concerto'
        STATUT_CONCERTO_FUTUR      = 'a_statut_concerto_futur'
        DATE_STATUT_CONCERTO       = 'a_date_statut_concerto'
        DATE_STATUT_CONCERTO_FUTUR = 'a_date_statut_concerto_futur'
        # relations many-to-many
        M2M_COMPETENCES            = 'a_liste_id_competences'
        M2M_PAM                    = 'pam'
        # relations one-to-many
        O2M_FMOB                   = 'fmobs'
        # relations one-to-one ou many-to-one
        REL_DOMAINE                = 'a_domaine'
        REL_FILIERE                = 'a_filiere'
        REL_DECISION               = 'decision'
        REL_FONCTION               = 'fonction'
        REL_FORMATION_EMPLOI       = 'formation_emploi'
        REL_GRADE                  = 'grade'
        REL_SOUS_VIVIER            = 'sous_vivier'
        STATUT_PAM                 = 'a_statut_pam'
        REL_PAM_INTER              = 'administre'


    objects = BulkUpdateOrCreateQuerySet.as_manager()

    a_id_sap = models.IntegerField(primary_key=True)
    formation_emploi = models.ForeignKey(FormationEmploi, related_name="formation_emploi", on_delete=models.SET_NULL,
                                         null=True, blank=True)
    pam = models.ManyToManyField(PAM, through='Administres_Pams')
    fonction = models.ForeignKey(Fonction, on_delete=models.SET_NULL, null=True, blank=True)
    sous_vivier = models.ForeignKey(SousVivier, on_delete=models.SET_NULL, null=True, blank=True)
    grade = models.ForeignKey(Grade, on_delete=models.SET_NULL, null=True, blank=True)
    a_grade_date_debut = models.CharField(max_length=100, null=True, blank=True)
    a_liste_id_marques = models.CharField(max_length=100, null=True, blank=True)
    a_liste_id_competences = models.ManyToManyField(Competence, blank=True)
    a_nom = models.CharField(max_length=100)
    a_prenom = models.CharField(max_length=100)
    a_sexe = models.CharField(max_length=100, null=True, blank=True)
    a_id_def = models.CharField(max_length=100, null=True, blank=True)
    a_eip = models.CharField(max_length=100, null=True, blank=True)
    a_fonction = models.CharField(max_length=100, null=True, blank=True)
    a_code_fonction = models.CharField(max_length=100, null=True, blank=True)
    a_domaine = models.ForeignKey(Domaine, on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    a_filiere = models.ForeignKey(Filiere, on_delete=models.SET_NULL, null=True, blank=True, db_constraint=False)
    a_nf = models.CharField(max_length=100, null=True, blank=True, choices=NiveauFonctionnelChoices.choices)
    a_domaine_poste = models.ForeignKey(Domaine, related_name="domaine_poste", on_delete=models.SET_NULL, null=True, blank=True)
    a_filiere_poste = models.ForeignKey(Filiere, related_name="filiere_poste", on_delete=models.SET_NULL, null=True, blank=True)
    a_nf_poste = models.CharField(max_length=100, null=True, blank=True, choices=NiveauFonctionnelChoices.choices)
    a_categorie = models.CharField(max_length=5, null=True, blank=True)
    a_domaine_futur = models.ForeignKey(Domaine, related_name="domaine_futur", on_delete=models.SET_NULL, null=True, blank=True)
    a_filiere_futur = models.ForeignKey(Filiere, related_name="filiere_futur", on_delete=models.SET_NULL, null=True, blank=True)
    a_nf_futur = models.CharField(max_length=100, null=True, blank=True, choices=NiveauFonctionnelChoices.choices)
    a_domaine_gestion = models.CharField('Domaine de gestion (BVT)', max_length=100, null=True, blank=True)
    a_date_entree_service = models.CharField(max_length=100, null=True, blank=True)
    a_arme = models.CharField(max_length=100, null=True, blank=True)
    a_rg_origine_recrutement = models.CharField(max_length=100, null=True, blank=True)
    a_date_naissance = models.CharField(max_length=100, null=True, blank=True)
    a_diplome_hl = models.CharField(max_length=100, null=True, blank=True)
    a_dernier_diplome = models.CharField(max_length=100, null=True, blank=True)
    a_credo_fe = models.CharField(max_length=100, null=True, blank=True)
    a_date_arrivee_fe = models.CharField(max_length=100, null=True, blank=True)
    a_pos_statuaire = models.CharField(max_length=100, null=True, blank=True)
    a_date_pos_statuaire = models.CharField(max_length=100, null=True, blank=True)
    a_interruption_service = models.CharField(max_length=100, null=True, blank=True)
    a_situation_fam = models.CharField(max_length=100, null=True, blank=True)
    a_date_mariage = models.CharField(max_length=100, null=True, blank=True)
    a_nombre_enfants = models.IntegerField(null=True, blank=True)
    a_enfants = models.CharField(max_length=100, null=True, blank=True)
    a_date_rdc = models.DateField(null=True, blank=True)
    a_date_dernier_acr = models.DateField(null=True, blank=True)
    a_eis = models.CharField(max_length=100, null=True, blank=True)
    a_sap_conjoint = models.IntegerField(null=True, blank=True)
    a_flag_particulier = models.IntegerField(null=True, blank=True)
    a_notes_partagees = models.TextField(null=True, blank=True)
    a_eip_fiche_detaille = models.CharField(max_length=100, null=True, blank=True)
    a_eip_futur = models.CharField(max_length=100, null=True, blank=True)
    a_liste_depts_souhaites = models.CharField(max_length=100, null=True, blank=True)
    a_liste_zones_geographiques_shm = models.CharField(max_length=100, null=True, blank=True)
    a_pls_gb_max = models.IntegerField(null=True, blank=True)
    a_marqueur_pn = models.BooleanField(default=False, null=True)
    a_fonction1 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction2 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction3 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction4 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction5 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction6 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction7 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction8 = models.CharField(max_length=100, null=True, blank=True)
    a_fonction9 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction1 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction2 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction3 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction4 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction5 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction6 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction7 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction8 = models.CharField(max_length=100, null=True, blank=True)
    a_date_fonction9 = models.CharField(max_length=100, null=True, blank=True)
    a_profession_conjoint = models.CharField(max_length=100, null=True, blank=True)
    a_id_def_conjoint = models.CharField(max_length=100, null=True, blank=True)
    a_notes_gestionnaire = models.TextField(null=True, blank=True)
    a_statut_pam = models.CharField('statut PAM', max_length=100,choices=StatutPamChoices.choices, null=True, blank=True)
    a_sexe_conjoint =  models.CharField(max_length=100, null=True, blank=True)
    a_lien_service = models.CharField(max_length=100, null=True, blank=True)
    a_age_en_annees = models.CharField(max_length=100, null=True, blank=True)
    a_origine_recrutement = models.CharField(max_length=100, null=True, blank=True)
    statut_futur = models.CharField('statut futur', db_column='a_statut_futur', max_length=8, choices=StatutFuturChoices.choices, null=True, blank=True)
    date_statut_futur = models.DateField('date du statut futur', db_column='a_date_statut_futur', null=True, blank=True)
    a_statut_concerto = models.CharField('statut CONCERTO', max_length=1000, null=True, blank=True)
    a_date_statut_concerto = models.CharField('date du statut CONCERTO', max_length=32, null=True, blank=True)
    a_statut_concerto_futur = models.CharField('statut CONCERTO futur', max_length=100, null=True, blank=True)
    a_date_statut_concerto_futur = models.CharField('date du statut CONCERTO futur', max_length=32, null=True, blank=True)
    suivi_previsionnel_situation = models.CharField('suivi prévisionnel de la situation du militaire', max_length=100, choices=SuiviPrevisionnelMilitaire.choices, null=True, blank=True)
    date_suivi_previsionnel_situation = models.DateField('date du suivi prévisionnel de la situation du militaire', db_column='a_date_suivi_previsionnel_situation', null=True, blank=True)
    a_annee_previsible_mutation = models.PositiveIntegerField('année prévisible de mutation', null=True, blank=True)
    a_fud = models.CharField('FUD de départ', max_length=32, null=True, blank=True)
    a_date_fud = models.CharField('date du FUD', max_length=32, null=True, blank=True)
    a_ciat = models.BooleanField('CIAT', default=False, null=True)
    a_specifique = models.CharField('PPE / SHM / ITD', max_length=250, choices=SpecifiqueChoices.choices, null=True, blank=True)
    a_date_affectation = models.DateField("Date d'affectation", null=True, blank=True)

    class Meta:
        verbose_name = "Administré"
        verbose_name_plural = "Administrés"
        constraints = [
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_a_nf_valid',
                check=models.Q(a_nf__in=NiveauFonctionnelChoices.values)
            ),
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_a_nf_poste_valid',
                check=models.Q(a_nf_poste__in=NiveauFonctionnelChoices.values)
            ),
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_a_nf_futur_valid',
                check=models.Q(a_nf_futur__in=NiveauFonctionnelChoices.values)
            ),
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_statut_pam_valid',
                check=models.Q(a_statut_pam__in=StatutPamChoices.values)
            ),
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_statut_futur_valid',
                check=models.Q(statut_futur__in=StatutFuturChoices.values)
            ),
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_a_specifique',
                check=models.Q(a_specifique__in=SpecifiqueChoices.values)
            )
        ]

class Administres_Pams(models.Model):
    class Cols():
        """ Constantes pour les noms de colonnes """

        PK                         = 'id'
        REL_PAM                    = 'pam'
        REL_ADMINISTRE             = 'administre'
        O2M_FMOB                   = 'fmobs'
        REL_DECISION               = 'decision'
        STATUT_PAM                 = 'a_statut_pam_annee'

    objects = BulkUpdateOrCreateQuerySet.as_manager()

    id = models.CharField(primary_key=True, max_length=100)
    pam = models.ForeignKey(PAM, on_delete = models.CASCADE, related_name='pam')
    administre = models.ForeignKey(Administre, on_delete=models.CASCADE, related_name=Administre.Cols.REL_PAM_INTER)
    a_statut_pam_annee = models.CharField('statut PAM', max_length=100,choices=StatutPamChoices.choices, null=True, blank=True)
    notes_pam = models.TextField(null=True, blank=True)
    a_ciat_pam = models.BooleanField('CIAT', default=False, null=True)
    a_specifique_pam = models.CharField('PPE / SHM / ITD', max_length=250, choices=SpecifiqueChoices.choices, null=True, blank=True)
    a_liste_depts_souhaites_pam = models.CharField(max_length=100, null=True, blank=True)
    a_liste_zones_geographiques_shm_pam = models.CharField(max_length=100, null=True, blank=True)
    a_situationfuture_notes_fe = models.TextField(null=True, blank=True)




class Affectation(models.Model):
    """
    Modèle pour les affectations
    """
    administre = models.ForeignKey(Administre, on_delete=models.CASCADE, null=True, blank=True, related_name='adm_affec', db_constraint=False)
    affect_libelle = models.CharField(max_length=100, null=True, blank=True)
    affect_date = models.CharField(max_length=100, null=True, blank=True)


class Diplome(models.Model):
    """
    Modèle pour les diplomes
    """

    administre = models.ForeignKey(Administre, on_delete=models.CASCADE, null=True, blank=True, related_name='adm_dip', db_constraint=False)
    diplome_libelle = models.CharField(max_length=100, null=True, blank=True)
    diplome_date = models.CharField(max_length=100, null=True, blank=True)
    diplome_note = models.CharField(max_length=100, null=True, blank=True)
    diplome_niveau = models.CharField(max_length=100, null=True, blank=True)


class FUD(models.Model):
    """
    Modèle pour les formulaires unitaires de demandes
    """

    administre = models.ForeignKey(Administre, on_delete=models.CASCADE, null=True, blank=True, related_name='adm_fud', db_constraint=False)
    fud_libelle = models.CharField(max_length=100, null=True, blank=True)
    fud_date_debut = models.CharField(max_length=100, null=True, blank=True)
    fud_date_fin = models.CharField(max_length=100, null=True, blank=True)
