import json
import random
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import status
from django.views.decorators.csrf import csrf_exempt

from DRIVERZZ_CUSTOMER_APIS import settings
from .helpers import send_otp_to_phone
from .auth import generate_access_token, verify_token
from .models import User


# Create your views here.

@csrf_exempt
def requestOtp(request):
    try:
        request_data = json.loads(request.body)
        otp = ''.join((random.choice('1234567890') for i in range(4)))
        otp = "1234"
        response = send_otp_to_phone(request_data['mobile'], otp)
        if response:
            request.session['otp'] = otp
            request.session['mobile'] = request_data['mobile']
            return JsonResponse({'message' : "OTP sent to registered Mobile", 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        else:
            return JsonResponse({'message' : "Something went wrong. Please try again later.", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
    except Exception as e:
        return JsonResponse({'message' : "Something went wrong. Please try again later.", 'status':status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)


@csrf_exempt
def verifyOtp(request):
    request_data = json.loads(request.body)        
    otp = request_data['otp']
    if(otp == request.session['otp']):
        try:
            user = User.objects.get(mobile = request.session['mobile'], user_type = 2)
        except User.DoesNotExist:
            user = User()
            user.mobile = request.session['mobile']
            username = ''.join((random.choice('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ') for i in range(6)))
            user.username = 'USER-' + username
            user.is_active = 1
            user.is_staff = 0
            user.is_superuser = 0
            user.user_type = 2
            user.save()

        token = generate_access_token(user)
        return JsonResponse({'message': "Otp Matched successfully", 'token' : token, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)    
    else:
        return JsonResponse({'message': "Otp not Matched",'status':status.HTTP_400_BAD_REQUEST},safe=False, status=status.HTTP_400_BAD_REQUEST)
          

def showDelete(request):
    return JsonResponse({'show_delete' : settings.SHOW_DELETE, 'status':status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)


@csrf_exempt  
def deleteAccount(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED},  safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            User.objects.get(id = user_id).delete()
        except Exception as e:
            print(e)
        return JsonResponse({'message' : "Account deleted successfully.", 'status':status.HTTP_200_OK},  safe=False, status=status.HTTP_200_OK)


@csrf_exempt
def verifyUser(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED},  safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            user = User.objects.get(id = user_id)
            return JsonResponse({'message' : "user is verified.", 'name' : user.name, 'email' : user.email, 'mobile' : user.mobile, 'status':status.HTTP_200_OK},  safe=False, status=status.HTTP_200_OK)
        except User.DoesNotExist:
            return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED},  safe=False, status=status.HTTP_401_UNAUTHORIZED)
        


@csrf_exempt
def profile(request):
    user_id = verify_token(request)
    if user_id == None:
        return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED},  safe=False, status=status.HTTP_401_UNAUTHORIZED)
    else:
        try:
            request_data = json.loads(request.body)
            user = User.objects.get(id = user_id)
            user.name = request_data['name']
            user.email = request_data['email']
            user.save()
            return JsonResponse({'message' : "Profile updated successfully.", 'status':status.HTTP_200_OK},  safe=False, status=status.HTTP_200_OK)
        except User.DoesNotExist:
            return JsonResponse({'message' : "User not logged in.", 'status':status.HTTP_401_UNAUTHORIZED},  safe=False, status=status.HTTP_401_UNAUTHORIZED)