# Path: backend/api/serializers/category.py
# class CategoryListSerializer(HyperlinkedModelSerializer):
#     class Meta:
#         model = Category
#         fields = ('name', 'slug')
# 
# class CategoryDetailSerializer(CategoryListSerializer):
#     photos = PhotoSerializer(many=True)
#     class Meta(CategoryListSerializer.Meta):
#         fields = ('name', 'slug', 'photos')
#         depth = 1
# 
# Path: backend/api/viewsets/utils.py
# class MultiSerializerViewSet(ModelViewSet):
#     serializers = {
#         'default': None,
#     }
# 
#     def get_serializer_class(self):
#         return self.serializers.get(self.action, self.serializers['default'])


from ..serializers import CategoryListSerializer, CategoryDetailSerializer
from rest_framework.viewsets import ModelViewSet
from photos.models import Category
from django.db.models import Count
from .utils import MultiSerializerViewSet


class CategoryViewSet(MultiSerializerViewSet):
    model = Category
    queryset = Category.objects.annotate(photos_count=Count('photo'))\
        .filter(photos_count__gte=1, is_enabled=True, )

    serializers = {
        'default': CategoryListSerializer,
        'retrieve': CategoryDetailSerializer,
    }

    lookup_field = 'slug'

# Path: backend/api/serializers/category.py
# class CategoryListSerializer(HyperlinkedModelSerializer):
#     class Meta:
#         model = Category
#         fields = ('name', 'slug')
# 
# class CategoryDetailSerializer(CategoryListSerializer):
#     photos = PhotoSerializer(many=True)
#     class Meta(CategoryListSerializer.Meta):
#         fields = ('name', 'slug', 'photos')
#         depth = 1
# 
# Path: backend/api/viewsets/utils.py
# class MultiSerializerViewSet(ModelViewSet):
#     serializers = {
#         'default': None,
#     }
# 
#     def get_serializer_class(self):
#         return self.serializers.get(self.action, self.serializers['default'])


# Path: backend/api/viewsets/category.py
from ..serializers import CategoryListSerializer, CategoryDetailSerializer
from rest_framework.viewsets import ModelViewSet
from photos.models import Category
from django.db.models import Count
from .utils import MultiSerializerViewSet



class CategoryViewSet(MultiSerializerViewSet):
    model = Category
    queryset = Category.objects.annotate(photos_count=Count('photo'))\
        .filter(photos_count__gte=1, is_enabled=True, )

    serializers = {
        'default': CategoryListSerializer,
        'retrieve': CategoryDetailSerializer,
    }

    lookup_field = 'slug'

    lookup_value_regex = '[0-9a-z\-_]+'