import json
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import status
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views import View
from authentication.auth import verify_token
from user_vehicles.models import User_Vehicles
from .serializers import VehiclesSerializer

# Create your views here.
@method_decorator(csrf_exempt, name='dispatch')
class Vehicles(View):
    def get(self, 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:
            vehicles = User_Vehicles.objects.filter(user_id = user_id)
            serializer = VehiclesSerializer(vehicles, many=True)
            return JsonResponse({'vehicles': serializer.data, 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)


    def post(self, 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:
            request_data = json.loads(request.body)
            vehicle = User_Vehicles()
            vehicle.user_id = user_id
            vehicle.vehicle_model_id = request_data['vehicle_model']
            vehicle.vehicle_model_name = request_data['vehicle_model_name']
            vehicle.vehicle_type_id = request_data['vehicle_type']
            vehicle.vehicle_type_name = request_data['vehicle_type_name']
            vehicle.transmission_type_id = request_data['transmission_type']
            vehicle.transmission_type_name = request_data['transmission_type_name']
            vehicle.vehicle_name = request_data['vehicle_name']
            vehicle.vehicle_no = request_data['vehicle_no']
            vehicle.save()
            return JsonResponse({'message': "Vehicle added successfully.", 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        


@method_decorator(csrf_exempt, name='dispatch')
class VehicleDelete(View):
    def post(self, 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)
                vehicle = User_Vehicles.objects.get(user_id = user_id, id = request_data['id'])
                vehicle.delete()
                return JsonResponse({'message': "Vehicle deleted successfully.", 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
            except User_Vehicles.DoesNotExist:
                return JsonResponse({'message': "Something went wrong. Please try again later.", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
            


@method_decorator(csrf_exempt, name='dispatch')
class VehicleDefault(View):
    def post(self, 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:
            User_Vehicles.objects.filter(user_id=user_id).update(is_default = 0)
            try:
                request_data = json.loads(request.body)
                vehicle = User_Vehicles.objects.get(user_id=user_id, id = request_data['id'])
                vehicle.is_default = 1
                vehicle.save()
                return JsonResponse({'message': "Vehicle set as default vehicle successfully.", 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
            except User_Vehicles.DoesNotExist:
                return JsonResponse({'message': "Something went wrong. Please try again later.", 'status': status.HTTP_400_BAD_REQUEST}, safe=False, status=status.HTTP_400_BAD_REQUEST)
            
