from authentication.auth import verify_token
import json
from django.http import JsonResponse
from rest_framework import status
from .models import VehicleModels, VehicleTypes, TransmissionTypes
from .serializers import VehicleTypesSerializer, VehicleModelsSerializer, VehicleTransmissionTypesSerializer
# Create your views here.

def vehicleTypes(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:
            vehicle_types = VehicleTypes.objects.all()
            serializer = VehicleTypesSerializer(vehicle_types, many=True)
            return JsonResponse({'vehicle_types': serializer.data, 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        except Exception as e:
            print(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)                



def vehicleModels(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:
            vehicle_models = VehicleModels.objects.all()
            serializer = VehicleModelsSerializer(vehicle_models, many=True)
            return JsonResponse({'vehicle_models': serializer.data, 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        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)                



def transmissionTypes(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:
            transmission_types = TransmissionTypes.objects.all()
            serializer = VehicleTransmissionTypesSerializer(transmission_types, many=True)
            return JsonResponse({'transmission_types': serializer.data, 'status': status.HTTP_200_OK}, safe=False, status=status.HTTP_200_OK)
        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)                

