# Path: webapp/api/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     appointment_set = AppointmentSerializer(many=True, read_only=True)
#     #location_set = LocationSerializer(many=True, read_only=True)
#     membership_set = MembershipSerializer(many=True, read_only=True)
#     #membership_set = serializers.RelationsList()
# 
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'email', 'first_name', 'is_superuser', 'appointment_set', 'membership_set')
# 
# class LocationSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = Location
#         fields = ('name', 'type', 'latitude', 'longitude', 'organization',
#                   'direction1', 'direction1_label', 'direction2', 'direction2_label')
# 
# class AppointmentSerializer(serializers.ModelSerializer):
#     location = LocationSerializer()
# 
#     class Meta:
#         model = Appointment
#         ordering = ['actual_end']
#         fields = ('id', 'user', 'location', 'scheduled_start', 'actual_start', 'actual_end','status')


from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from .serializers import UserSerializer, LocationSerializer, AppointmentSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import detail_route  # action
from main.models import Organization, OrganizationMetrics, Location, Appointment, Survey, SurveyValue, Metric, Value, \
    Event, SurveyEvent
from main.logic import *
from django.http import JsonResponse
from main.views import RequestCSVView
import datetime
import json
        if request.user.is_authenticated():
            serializer = UserSerializer(request.user)
            return Response(serializer.data)
        else:
            return Response(None, status=status.HTTP_401_UNAUTHORIZED)

class singleCompletedAppt(viewsets.ModelViewSet):
    # Trying to create a new view to support showing a single appointment's stats highlights on the mobile app's "completed survey" view
    # this is associated to the #appstates template fragment in the index.html file


    queryset = Appointment.objects.all()
    #serializer_class = Stats_for_appt_Serializer
    serializer_class = AppointmentSerializer

    @detail_route(methods=['GET'])
    def get(self, request, pk=None):
        if request.user.is_authenticated():
            appt = self.get_object()
            theStats = stats_for_appt(appt)
            #serializer = Stats_for_appt_Serializer(theStats)

            return Response(theStats, status=status.HTTP_200_OK)
        else:
            return Response(None, status=status.HTTP_401_UNAUTHORIZED)



class ApptViewSet(viewsets.ModelViewSet):
    queryset = Appointment.objects.all()

# Path: webapp/api/serializers.py
# class UserSerializer(serializers.ModelSerializer):
#     appointment_set = AppointmentSerializer(many=True, read_only=True)
#     #location_set = LocationSerializer(many=True, read_only=True)
#     membership_set = MembershipSerializer(many=True, read_only=True)
#     #membership_set = serializers.RelationsList()
# 
#     class Meta:
#         model = User
#         fields = ('id', 'username', 'email', 'first_name', 'is_superuser', 'appointment_set', 'membership_set')
# 
# class LocationSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = Location
#         fields = ('name', 'type', 'latitude', 'longitude', 'organization',
#                   'direction1', 'direction1_label', 'direction2', 'direction2_label')
# 
# class AppointmentSerializer(serializers.ModelSerializer):
#     location = LocationSerializer()
# 
#     class Meta:
#         model = Appointment
#         ordering = ['actual_end']
#         fields = ('id', 'user', 'location', 'scheduled_start', 'actual_start', 'actual_end','status')


# Path: webapp/api/views.py
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from .serializers import UserSerializer, LocationSerializer, AppointmentSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import detail_route  # action
from main.models import Organization, OrganizationMetrics, Location, Appointment, Survey, SurveyValue, Metric, Value, \
    Event, SurveyEvent
from main.logic import *
from django.http import JsonResponse
from main.views import RequestCSVView
import datetime
import json

        if request.user.is_authenticated():
            serializer = UserSerializer(request.user)
            return Response(serializer.data)
        else:
            return Response(None, status=status.HTTP_401_UNAUTHORIZED)

class singleCompletedAppt(viewsets.ModelViewSet):
    # Trying to create a new view to support showing a single appointment's stats highlights on the mobile app's "completed survey" view
    # this is associated to the #appstates template fragment in the index.html file


    queryset = Appointment.objects.all()
    #serializer_class = Stats_for_appt_Serializer
    serializer_class = AppointmentSerializer

    @detail_route(methods=['GET'])
    def get(self, request, pk=None):
        if request.user.is_authenticated():
            appt = self.get_object()
            theStats = stats_for_appt(appt)
            #serializer = Stats_for_appt_Serializer(theStats)

            return Response(theStats, status=status.HTTP_200_OK)
        else:
            return Response(None, status=status.HTTP_401_UNAUTHORIZED)



class ApptViewSet(viewsets.ModelViewSet):
    queryset = Appointment.objects.all()

    serializer_class = AppointmentSerializer