from django.http.response import JsonResponse
from rest_framework.views import APIView
from rest_framework.parsers import JSONParser

from .models import *

from GestionMessagerie.serializers import MessageSerializer

from N3wtSchool import bdd

class MessagerieView(APIView):
    def get(self, request, profile_id):
        messagesList = bdd.getObjects(_objectName=Messagerie, _columnName='destinataire__id', _value=profile_id)
        messages_serializer = MessageSerializer(messagesList, many=True)
        return JsonResponse(messages_serializer.data, safe=False)

class MessageView(APIView):
    def post(self, request):
        message_data=JSONParser().parse(request)
        message_serializer = MessageSerializer(data=message_data)

        if message_serializer.is_valid():
            message_serializer.save()

            return JsonResponse('Nouveau Message ajouté', safe=False)

        return JsonResponse(message_serializer.errors, safe=False)

class MessageSimpleView(APIView):
    def get(self, request, id):
        message=bdd.getObject(Messagerie, "id", id)
        message_serializer=MessageSerializer(message)
        return JsonResponse(message_serializer.data, safe=False)

