# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

from api.viewsets import ReadProtectedModelViewSet
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter

from .serializers import AllergenSerializer, FoodSerializer, BasicFoodSerializer, TransformedFoodSerializer, QRCodeSerializer, \
    DishSerializer, SupplementSerializer, OrderSerializer, FoodTransactionSerializer
from ..models import Allergen, Food, BasicFood, TransformedFood, QRCode, Dish, Supplement, Order, FoodTransaction


class AllergenViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `Allergen` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/allergen/
    """
    queryset = Allergen.objects.order_by('id')
    serializer_class = AllergenSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['name', ]
    search_fields = ['$name', ]


class FoodViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `Food` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/food/
    """
    queryset = Food.objects.order_by('id')
    serializer_class = FoodSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['name', ]
    search_fields = ['$name', ]


class BasicFoodViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `BasicFood` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/basicfood/
    """
    queryset = BasicFood.objects.order_by('id')
    serializer_class = BasicFoodSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['name', ]
    search_fields = ['$name', ]


class TransformedFoodViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `TransformedFood` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/transformedfood/
    """
    queryset = TransformedFood.objects.order_by('id')
    serializer_class = TransformedFoodSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['name', ]
    search_fields = ['$name', ]


class QRCodeViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `QRCode` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/qrcode/
    """
    queryset = QRCode.objects.order_by('id')
    serializer_class = QRCodeSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['qr_code_number', ]
    search_fields = ['$qr_code_number', ]


class DishViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `Dish` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/dish/
    """
    queryset = Dish.objects.order_by('id')
    serializer_class = DishSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['main__name', 'activity', ]
    search_fields = ['$main__name', '$activity', ]


class SupplementViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `Supplement` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/supplement/
    """
    queryset = Supplement.objects.order_by('id')
    serializer_class = SupplementSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['food__name', 'dish__activity', ]
    search_fields = ['$food__name', '$dish__activity', ]


class OrderViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `Order` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/order/
    """
    queryset = Order.objects.order_by('id')
    serializer_class = OrderSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['user', 'activity', 'dish', 'supplements', 'number', ]
    search_fields = ['$user', '$activity', '$dish', '$supplements', '$number', ]


class FoodTransactionViewSet(ReadProtectedModelViewSet):
    """
    REST API View set.
    The djangorestframework plugin will get all `FoodTransaction` objects, serialize it to JSON with the given serializer,
    then render it on /api/food/foodtransaction/
    """
    queryset = FoodTransaction.objects.order_by('id')
    serializer_class = FoodTransactionSerializer
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = ['order', ]
    search_fields = ['$order', ]
