from django.shortcuts import render
from rest_framework import status
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
from authentication.auth import verify_token
from cancel_reasons.models import CancelReasons
from cancel_reasons.serializer import CancelReasonsSerializer


# Create your views here.

@csrf_exempt
def cancelReasons(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message':"User is not login", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        if request.method == 'POST':
            request_data = json.loads(request.body)
            cancel_reason = CancelReasons()
            cancel_reason.title = request_data['title']
            cancel_reason.reason_for = int(request_data['reason_for'])
            cancel_reason.save()
            return JsonResponse({'message':"Cancel Reason added successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        
        else:
            cancel_reason = CancelReasons.objects.all()
            serializer = CancelReasonsSerializer(cancel_reason, many=True)
            return JsonResponse({'cancel_reasons': serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
            

@csrf_exempt
def editCancelReason(request):
    if request.method == 'POST':
        request_data = json.loads(request.body)
        if request_data['id']:
            cancel_reason = CancelReasons.objects.get(id = request_data['id'])
            cancel_reason.title = request_data['title']
            cancel_reason.reason_for = int(request_data['reason_for'])
            cancel_reason.save()
            return JsonResponse({'message': "Cancel reason updated successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
         return JsonResponse({'message':"Cancel reason  id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        

    else:
        if request.GET.get('id'):
            try:
                cancel_reason = CancelReasons.objects.get(id = request.GET.get('id'))
                serializer = CancelReasonsSerializer(cancel_reason, many=False)
                return JsonResponse({'cancel_reason': serializer.data,'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)   
            except CancelReasons.DoesNotExist:
                return JsonResponse({'message':"Cancel reason doesn't exist", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        else:
            return JsonResponse({'message':"Cancel reason id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)        

                

@csrf_exempt
def deleteCancelReason(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message': "User not login.", 'status':status.HTTP_401_UNAUTHORIZED}, safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            request_data = json.loads(request.body)
            CancelReasons.objects.get(id = request_data['id']).delete()
            return JsonResponse({'message':"Cancel reason delete Successfully", 'status':status.HTTP_200_OK})
        except CancelReasons.DoesNotExist:
            return JsonResponse({'message':"Cancel reason doesn't exists, Invalid Cancel reason id ", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
 

