from rest_framework import generics, permissions, status
from rest_framework.response import Response
from ..models import Suggestion
from ..serializers import SuggestionSerializer

class SuggestionListCreateView(generics.ListCreateAPIView):
    queryset = Suggestion.objects.all()
    serializer_class = SuggestionSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(suggested_by=self.request.user)

class SuggestionApproveView(generics.UpdateAPIView):
    queryset = Suggestion.objects.all()
    serializer_class = SuggestionSerializer
    permission_classes = [permissions.IsAdminUser]

    def update(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.is_approved = True
        instance.save()
        return Response({"message": "Suggestion approved"})