from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
from rest_framework import viewsets
from apps.livestock import models as livestock_models
from apps.tag.web.api.v1.api import TagViewSet
from . import serializers as livestock_serializers
from rest_framework.exceptions import APIException
from rest_framework.decorators import action
from rest_framework.response import Response
from common.tools import CustomOperations
from django.db import transaction
from rest_framework import status


def trash(queryset, pk):
    """ sent object to trash """
    obj = queryset.get(id=pk)
    obj.trash = True
    obj.save()


def delete(queryset, pk):
    """ full delete object """
    obj = queryset.get(id=pk)
    obj.delete()


class LiveStockViewSet(viewsets.ModelViewSet, SoftDeleteMixin):  # noqa
    queryset = livestock_models.LiveStock.objects.all()
    serializer_class = livestock_serializers.LiveStockSerializer

    @action(
        methods=['put'],
        detail=True,
        url_path='trash',
        url_name='trash',
        name='trash',
    )
    @transaction.atomic
    def trash(self, request, pk=None):
        """ Sent livestock to trash """
        try:
            trash(self.queryset, pk)
        except APIException as e:
            return Response(e, status.HTTP_204_NO_CONTENT)

    @action(
        methods=['post'],
        detail=True,
        url_name='delete',
        url_path='delete',
        name='delete'
    )
    @transaction.atomic
    def delete(self, request, pk=None):
        """ Full delete of livestock object """
        try:
            delete(self.queryset, pk)
            return Response(status=status.HTTP_200_OK)
        except APIException as e:
            return Response(e, status=status.HTTP_204_NO_CONTENT)


class LiveStockTypeViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
    queryset = livestock_models.LiveStockType.objects.all()
    serializer_class = livestock_serializers.LiveStockTypeSerializer

    @action(
        methods=['put'],
        detail=True,
        url_path='trash',
        url_name='trash',
        name='trash',
    )
    @transaction.atomic
    def trash(self, request, pk=None):
        """ Sent livestock type to trash """
        try:
            trash(self.queryset, pk)
        except APIException as e:
            return Response(e, status.HTTP_204_NO_CONTENT)

    @action(
        methods=['post'],
        detail=True,
        url_name='delete',
        url_path='delete',
        name='delete'
    )
    @transaction.atomic
    def delete(self, request, pk=None):
        """ Full delete of livestock type object """
        try:
            delete(self.queryset, pk)
            return Response(status=status.HTTP_200_OK)
        except APIException as e:
            return Response(e, status=status.HTTP_204_NO_CONTENT)


class LiveStockUseTypeViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
    queryset = livestock_models.LiveStockUseType.objects.all()
    serializer_class = livestock_serializers.LiveStockUseTypeSerializer

    @action(
        methods=['put'],
        detail=True,
        url_path='trash',
        url_name='trash',
        name='trash',
    )
    @transaction.atomic
    def trash(self, request, pk=None):
        """ Sent livestock use type to trash """
        try:
            trash(self.queryset, pk)
        except APIException as e:
            return Response(e, status.HTTP_204_NO_CONTENT)

    @action(
        methods=['post'],
        detail=True,
        url_name='delete',
        url_path='delete',
        name='delete'
    )
    @transaction.atomic
    def delete(self, request, pk=None):
        """ Full delete of livestock use type object """
        try:
            delete(self.queryset, pk)
            return Response(status=status.HTTP_200_OK)
        except APIException as e:
            return Response(e, status=status.HTTP_204_NO_CONTENT)


class LiveStockSpeciesViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
    queryset = livestock_models.LiveStockSpecies.objects.all()
    serializer_class = livestock_serializers.LiveStockSpeciesSerializer

    @action(
        methods=['put'],
        detail=True,
        url_path='trash',
        url_name='trash',
        name='trash',
    )
    @transaction.atomic
    def trash(self, request, pk=None):
        """ Sent species to trash """
        try:
            trash(self.queryset, pk)
            return Response(status=status.HTTP_200_OK)
        except APIException as e:
            return Response(status=status.HTTP_403_FORBIDDEN)

    @action(
        methods=['post'],
        detail=True,
        url_name='delete',
        url_path='delete',
        name='delete'
    )
    @transaction.atomic
    def delete(self, request, pk=None):
        """ Full delete of species object """
        try:
            delete(self.queryset, pk)
            return Response(status=status.HTTP_200_OK)
        except APIException as e:
            return Response(e, status=status.HTTP_204_NO_CONTENT)
