from rest_framework import viewsets

from core.apps.eggs.models import History
from core.apps.eggs.serializers import HistorySerializer
from core.http.permissions import IsRole


class HistoryViewSet(viewsets.ModelViewSet):
    serializer_class = HistorySerializer
    http_method_names = ["get"]
    permission_classes = [IsRole(["admin", "sklad", "courier", "market"])]

    def get_queryset(self):
        user = self.request.user
        action_param = self.request.query_params.get("action", None)

        if user.role == "admin":
            queryset = History.objects.all()
        else:
            queryset = History.objects.filter(user_id=user.id)

        if action_param:
            queryset = queryset.filter(action=action_param)

        return queryset
