from django.shortcuts import render
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework import status
from django.db import transaction
from .models import EmployeeType
from .serializers import EmployeeTypeSerializer
from ErrorLogs.utils import log_error

# Create your views here.

@api_view(['GET', 'POST'])
@permission_classes([AllowAny])
def employee_type_list(request):
    if request.method == 'GET':
        try:
            employee_types = EmployeeType.objects.all()
            serializer = EmployeeTypeSerializer(employee_types, many=True)
            return Response(serializer.data)
        except Exception as e:
            log_error('employee_type_list.GET', e)
            return Response(
                {"error": "An error occurred while fetching employee types"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )
    
    elif request.method == 'POST':
        try:
            with transaction.atomic():
                serializer = EmployeeTypeSerializer(data=request.data)
                if serializer.is_valid():
                    employee_type = serializer.save()
                    return Response(EmployeeTypeSerializer(employee_type).data, status=status.HTTP_201_CREATED)
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            log_error('employee_type_list.POST', e)
            return Response(
                {"error": "An error occurred while creating employee type"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

@api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([AllowAny])
def employee_type_detail(request, pk):
    try:
        employee_type = EmployeeType.objects.get(pk=pk)
    except EmployeeType.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    except Exception as e:
        log_error('employee_type_detail.get_employee_type', e)
        return Response(
            {"error": "An error occurred while fetching employee type"},
            status=status.HTTP_500_INTERNAL_SERVER_ERROR
        )

    if request.method == 'GET':
        try:
            serializer = EmployeeTypeSerializer(employee_type)
            return Response(serializer.data)
        except Exception as e:
            log_error('employee_type_detail.GET', e)
            return Response(
                {"error": "An error occurred while fetching employee type details"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

    elif request.method == 'PUT':
        try:
            with transaction.atomic():
                serializer = EmployeeTypeSerializer(employee_type, data=request.data)
                if serializer.is_valid():
                    updated_employee_type = serializer.save()
                    return Response(EmployeeTypeSerializer(updated_employee_type).data)
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            log_error('employee_type_detail.PUT', e)
            return Response(
                {"error": "An error occurred while updating employee type"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

    elif request.method == 'DELETE':
        try:
            with transaction.atomic():
                employee_type.delete()
                return Response(status=status.HTTP_204_NO_CONTENT)
        except Exception as e:
            log_error('employee_type_detail.DELETE', e)
            return Response(
                {"error": "An error occurred while deleting employee type"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )
