import json
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import status
from django.views.decorators.csrf import csrf_exempt


from authentication.auth import verify_token
from coupons.models import Coupons
from coupons.serializer import CouponSerializer

# Create your views here.


@csrf_exempt
def coupons(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)
            coupon = Coupons()
            coupon.title = request_data['title']
            coupon.code = request_data['code']
            coupon.coupon_type = int(request_data['coupon_type'])
            coupon.value = request_data['value']
            coupon.max_value = request_data['max_value']
            coupon.count = request_data['count']
            coupon.description = request_data['description']
            coupon.save()
            return JsonResponse({'message': "Coupon added successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            coupon = Coupons.objects.all()
            serializer = CouponSerializer(coupon, many=True)
            return JsonResponse({'coupons':serializer.data, 'status':status.HTTP_200_OK},safe=False, status=status.HTTP_200_OK)
            

@csrf_exempt     
def editCoupon(request):
    if request.method == 'POST':
        request_data = json.loads(request.body)
        if request_data['id']:
            coupon = Coupons.objects.get(id=request_data['id'])
            coupon.title = request_data['title']
            coupon.code = request_data['code']
            coupon.coupon_type = int(request_data['coupon_type'])
            coupon.value = request_data['value']
            coupon.max_value = request_data['max_value']
            coupon.count = request_data['count']
            coupon.description = request_data['description']
            coupon.save()
            return JsonResponse({'message' : "Coupon edited Successfully", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            return JsonResponse({'message':"Coupon id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
    
    else:        
        if request.GET.get('id'):
            try:
                coupon = Coupons.objects.get(id=request.GET.get('id'))
                serializer = CouponSerializer(coupon, many=False) 
                return JsonResponse({'coupon': serializer.data, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
            except Coupons.DoesNotExist:
                return JsonResponse({'message':"Coupon doesn't exists.", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        else:
            return JsonResponse({'message':"Coupon id is required", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
        


@csrf_exempt
def deleteCoupon(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)        
            Coupons.objects.get(id = request_data['id']).delete()
            return JsonResponse({'message': "Coupon deleted Successfully", 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        except Coupons.DoesNotExist:
            return JsonResponse({'message': "Coupon doesn't exists. Invalid coupon id", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)

    
