from django.db.models import Q

from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter, OpenApiResponse

from api.viewsets.common.tenancy import ModelListRetrieveDeleteViewSet

from core.models.ticket.ticket import Ticket
from core.serializers.ticket_related import (    # pylint: disable=W0611:unused-import
    RelatedTickets,
    RelatedTicketModelSerializer,
    RelatedTicketViewSerializer,
)



@extend_schema_view(
    destroy = extend_schema(
        summary = 'Delete a related ticket',
        description = '',
        parameters = [
            OpenApiParameter(
                name = 'ticket_id',
                location = 'path',
                type = int
            ),
        ],
        responses = {
            204: OpenApiResponse(description=''),
            403: OpenApiResponse(description='User is missing delete permissions'),
        }
    ),
    list = extend_schema(
        summary = 'Fetch all related tickets',
        description='',
        parameters = [
            OpenApiParameter(
                name = 'ticket_id',
                location = 'path',
                type = int
            ),
        ],
        responses = {
            200: OpenApiResponse(description='', response=RelatedTicketViewSerializer),
            403: OpenApiResponse(description='User is missing view permissions'),
        }
    ),
    retrieve = extend_schema(
        summary = 'Fetch a related ticket',
        description='',
        parameters = [
            OpenApiParameter(
                name = 'id',
                location = 'path',
                type = int
            ),
            OpenApiParameter(
                name = 'ticket_id',
                location = 'path',
                type = int
            ),
        ],
        responses = {
            200: OpenApiResponse(description='', response=RelatedTicketViewSerializer),
            403: OpenApiResponse(description='User is missing view permissions'),
        }
    ),
)
class ViewSet(ModelListRetrieveDeleteViewSet):


    filterset_fields = [
        'organization',
    ]

    search_fields = [
        'name',
    ]

    metadata_markdown = True

    model = RelatedTickets

    parent_model = Ticket

    parent_model_pk_kwarg = 'ticket_id'

    view_description: str = 'Related Tickets'


    def get_serializer_class(self):

        if (
            self.action == 'list'
            or self.action == 'retrieve'
        ):

            self.serializer_class = globals()[str( self.model._meta.verbose_name).replace(' ' , '') + 'ViewSerializer']

        else:

            self.serializer_class = globals()[str( self.model._meta.verbose_name).replace(' ' , '') + 'ModelSerializer']


        return self.serializer_class


    def get_queryset(self):

        if self.queryset is not None:

            return self.queryset

        self.queryset = RelatedTickets.user(
                    user = self.request.user, permission = self._permission_required
        ).objects.filter(
            Q(from_ticket_id_id=self.kwargs['ticket_id'])
                |
            Q(to_ticket_id_id=self.kwargs['ticket_id'])
        )

        self.queryset = self.queryset.filter().order_by('id')

        return self.queryset
