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 Supervisor
from .serializers import SupervisorSerializer
from ErrorLogs.utils import log_error

# Create your views here.

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

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

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

    elif request.method == 'PUT':
        try:
            with transaction.atomic():
                serializer = SupervisorSerializer(supervisor, data=request.data)
                if serializer.is_valid():
                    updated_supervisor = serializer.save()
                    return Response(SupervisorSerializer(updated_supervisor).data)
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        except Exception as e:
            log_error('supervisor_detail.PUT', e)
            return Response(
                {"error": "An error occurred while updating supervisor"},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

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