from rest_framework import generics, permissions
from rest_framework.exceptions import NotFound

from tickets.models import Ticket
from tickets.api.serializers import TicketDetailSerializer, TicketSerializer, TicketStatusSerializer
from tickets.api.permissions import UserTicketAccessPermission, UserTicketStatusAccessPermission


class TicketDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Ticket.objects.all()
    serializer_class = TicketDetailSerializer
    permission_classes = [permissions.IsAuthenticated & UserTicketAccessPermission]


class TicketCreateAPIView(generics.CreateAPIView):
    queryset = Ticket.objects.all()
    serializer_class = TicketSerializer
    permission_classes = [permissions.IsAuthenticated & UserTicketAccessPermission]

    def perform_create(self, serializer):
        if serializer.is_valid():
            serializer.save(created_by=self.request.user) 


class TicketStatusAPIView(generics.RetrieveUpdateAPIView):
    queryset = Ticket.objects.all()
    serializer_class = TicketStatusSerializer
    permission_classes = [permissions.IsAuthenticated & UserTicketStatusAccessPermission]