from rest_framework import serializers
from .models import Video, ViewRecord
from apps.system.serializers import UserSimpleSerializer

class VideoSerializer(serializers.ModelSerializer):
    category_name = serializers.StringRelatedField(source='category', read_only=True)
    class Meta:
        model = Video
        fields = '__all__'

class VideoSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Video
        fields = ['id', 'name', 'duration']
        
class VRecordSerializer(serializers.ModelSerializer):
    user_ = UserSimpleSerializer(source='user', read_only=True)
    video_ = VideoSimpleSerializer(source='video', read_only=True)
    class Meta:
        model = ViewRecord
        fields = '__all__'
    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('user', 'video')
        return queryset


class VRecordUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = ViewRecord
        fields=['num', 'current']