from django.shortcuts import render
from django.contrib.auth.models import User
from rest_framework import viewsets
from oauth2_provider.contrib.rest_framework import (
    TokenHasReadWriteScope,
)
from notification.models import NotificationType
from authentication.models import UserProfile, UserMessage
from panel.models import (
    PoultryRequest,
    PoultryAssignmentInformation,
    KillHouseCheckRequest,
    KillHouseRequest,
    ProvinceCheckOperatorRequest,
    OperatorLastTimeEnter,
    ReportingFieldRoleLimitation,
    Group
)
from panel.poultry.serializers import (
    PoultryRequestSerializer,
    PoultryAssignmentInformationSerializer
)
from panel.ReportingPanel.serializer import (
    ReportingFieldLimitationSerializer
)
from authentication.serializers import GroupSerializer
from rest_framework.response import Response
from rest_framework import status
from datetime import datetime


class OperatorLastTimeEnterViewSet(viewsets.ModelViewSet):
    queryset = OperatorLastTimeEnter.objects.all()
    permission_classes = [TokenHasReadWriteScope]
    serializer_class = []

    def update(self, request, *args, **kwargs):
        time = request.data['time']

        if UserProfile.objects.filter(user=request.user):
            if not OperatorLastTimeEnter.objects.filter(
                    operator__exact=UserProfile.objects.get(user__exact=request.user)):
                obj = OperatorLastTimeEnter()
            else:
                obj = OperatorLastTimeEnter.objects.get(
                    operator__exact=UserProfile.objects.get(user__exact=request.user))
            if UserProfile.objects.get(user=request.user).role.name == "CityOperator":
                obj.city_operator_last_time_check = time
                obj.operator = UserProfile.objects.get(user=request.user)
            elif UserProfile.objects.get(user=request.user).role.name == "ProvinceOperator":
                obj.province_operator_last_time_check = time
                obj.operator = UserProfile.objects.get(user=request.user)
            elif UserProfile.objects.get(user=request.user).role.name == "KillHouse":
                obj.kill_house_last_time_check = time
                obj.operator = UserProfile.objects.get(user=request.user)
        obj.save()
        return Response({'msg': 'Done'}, status=status.HTTP_200_OK)


class ReportingFieldLimitationViewSet(viewsets.ModelViewSet):
    queryset = ReportingFieldRoleLimitation.objects.all()
    serializer_class = ReportingFieldLimitationSerializer
    permission_classes = [TokenHasReadWriteScope]

    def create(self, request, *args, **kwargs):
        group = Group.objects.get(name__exact=request.data['role_name'])
        request.data.pop('role_name')
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            obj = serializer.create(validated_data=request.data)
            obj.role_name = group
            obj.save()
            serializer = self.serializer_class(obj)
            return Response(serializer.data)
        return Response(serializer.errors)


