from django.http.response import JsonResponse
from rest_framework.views import APIView
from django.utils import timezone

from .models import Planning, Events

from .serializers import PlanningSerializer, EventsSerializer

from N3wtSchool import bdd

class PlanningView(APIView):
    def get(self, request):
        plannings=bdd.getAllObjects(Planning)
        planning_serializer=PlanningSerializer(plannings, many=True)

        return JsonResponse(planning_serializer.data, safe=False)

    def post(self, request):
        planning_serializer = PlanningSerializer(data=request.data)
        if planning_serializer.is_valid():
            planning_serializer.save()
            return JsonResponse(planning_serializer.data, status=201)
        return JsonResponse(planning_serializer.errors, status=400)



class PlanningWithIdView(APIView):
    def get(self, request,id):
        planning = Planning.objects.get(pk=id)
        if planning is None:
            return JsonResponse({"errorMessage":'Le dossier d\'inscription n\'a pas été trouvé'}, safe=False, status=status.HTTP_404_NOT_FOUND)
        planning_serializer=PlanningSerializer(planning)

        return JsonResponse(planning_serializer.data, safe=False)

    def put(self, request, id):
        try:
            planning = Planning.objects.get(pk=id)
        except Planning.DoesNotExist:
            return JsonResponse({'error': 'Planning not found'}, status=404)

        planning_serializer = PlanningSerializer(planning, data=request.data)
        if planning_serializer.is_valid():
            planning_serializer.save()
            return JsonResponse(planning_serializer.data)
        return JsonResponse(planning_serializer.errors, status=400)

    def delete(self, request, id):
        try:
            planning = Planning.objects.get(pk=id)
        except Planning.DoesNotExist:
            return JsonResponse({'error': 'Planning not found'}, status=404)

        planning.delete()
        return JsonResponse({'message': 'Planning deleted'}, status=204)

class EventsView(APIView):
    def get(self, request):
        events = bdd.getAllObjects(Events)
        events_serializer = EventsSerializer(events, many=True)
        return JsonResponse(events_serializer.data, safe=False)

    def post(self, request):
        events_serializer = EventsSerializer(data=request.data)
        if events_serializer.is_valid():
            events_serializer.save()
            return JsonResponse(events_serializer.data, status=201)
        return JsonResponse(events_serializer.errors, status=400)

class EventsWithIdView(APIView):
    def put(self, request, id):
        try:
            event = Events.objects.get(pk=id)
        except Events.DoesNotExist:
            return JsonResponse({'error': 'Event not found'}, status=404)

        events_serializer = EventsSerializer(event, data=request.data)
        if events_serializer.is_valid():
            events_serializer.save()
            return JsonResponse(events_serializer.data)
        return JsonResponse(events_serializer.errors, status=400)

    def delete(self, request, id):
        try:
            event = Events.objects.get(pk=id)
        except Events.DoesNotExist:
            return JsonResponse({'error': 'Event not found'}, status=404)

        event.delete()
        return JsonResponse({'message': 'Event deleted'}, status=204)

class UpcomingEventsView(APIView):
    def get(self, request):
        current_date = timezone.now()
        upcoming_events = Events.objects.filter(start__gte=current_date)
        events_serializer = EventsSerializer(upcoming_events, many=True)
        return JsonResponse(events_serializer.data, safe=False)