from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.db.models import Q

from ..models import AvisPosteChoices as AvisPoste
from ..models import (Competence, Domaine, Filiere, FormationEmploi, Grade,
                      SousVivier, PAM,
                      Marque, MarquesGroupe, RefSvFil, SpecifiqueChoices)
from ..models import StatutFuturChoices as StatutFutur
from ..models import StatutPamChoices as StatutPam
from ..models import ZoneGeographique
from ..models import Fonction
from ..serializers import (ChoicesSerializer, RefAvisPosteChoicesSerializer,
                           RefStatutPamChoicesSerializer)
from ..serializers.commun import AssignmentState
from ..utils.decisions import get_all_decisions
from .commun import execution_time_viewset, query_count_viewset


@execution_time_viewset
@query_count_viewset
class ReferencesView(APIView):
    """
    Cette classe est dédiée au vue de la reference.
    """
    permission_classes = [IsAuthenticated]

    def get(self, request):
        """
        La fonction get envoie les données de référence

        :return: réponse contenant les données de référence
        """

        groupesMarques = [obj.as_dict() for obj in MarquesGroupe.objects.all().order_by('gm_code')]
        marques = [obj.as_dict() for obj in Marque.objects.all().order_by('mar_code')]
        competences = [obj.as_dict() for obj in Competence.objects.all().order_by('comp_id')]
        domaines = [obj.as_dict() for obj in Domaine.objects.all().order_by('d_code')]
        filieres = [obj.as_dict() for obj in Filiere.objects.all().order_by('domaine__d_code', 'f_code')]
        zones = [obj.as_dict() for obj in ZoneGeographique.objects.all()]
        # TODO: Vérifier la présence de l'ordre sur les postes'
        grades = [obj.as_dict() for obj in Grade.objects.all()]
        FEs = [obj.as_dict() for obj in FormationEmploi.objects.all().order_by('fe_mere_credo')]
        garnison_lieux = [obj.as_dict() for obj in FormationEmploi.objects.all().order_by('fe_mere_credo', 'fe_garnison_lieu')]
        RefSvFils_init = {obj.as_dict()['ref_sv_fil_code'] for obj in RefSvFil.objects.all().order_by('ref_sv_fil_code')}
        RefSvFils = [{'ref_sv_fil_code': obj} for obj in RefSvFils_init]
        Specifique = [{'code': i.value} for i in SpecifiqueChoices]
        SousViviers = [obj.as_dict() for obj in SousVivier.objects.filter(~Q(sv_id='BVT')).order_by('sv_libelle')]
        Pams = [obj.as_dict() for obj in PAM.objects.filter(~Q(pam_id='SORG')& Q(pam_statut__in=['PAM en cours', 'PAM A+1'])).order_by('pam_id')]
        Etr = [obj.as_dict() for obj in Fonction.objects.all()]
        return Response({
            'groupesMarques': groupesMarques,
            'marques': marques,
            'domaines': domaines,
            'filieres': filieres,
            'grades': grades,
            'competences': competences,
            'FEs': FEs,
            'statutFutur': ChoicesSerializer(StatutFutur, many=True).data,
            'Zones': zones,
            'decisions': ChoicesSerializer(get_all_decisions(), many=True).data,
            'avisAdministre': RefStatutPamChoicesSerializer(StatutPam, many=True).data,
            'avisPoste': RefAvisPosteChoicesSerializer(AvisPoste, many=True).data,
            'RefSvFils': RefSvFils,
            'Specifique': Specifique,
            'etatsFeBmob': tuple(e.value for e in AssignmentState),
            'sousViviers': SousViviers,
            'pams': Pams,
            'etr' : Etr,
        })
