from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from core.apps.eggs.models import Debt
from core.apps.eggs.serializers import DebtSerializer


class UserDebtsAPIView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request, id=None):
        if id:
            user_debts = Debt.objects.filter(market_id=id).order_by('-created_at')
        else:
            user_debts = Debt.objects.none()

        serializer = DebtSerializer(user_debts, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
