from rest_framework import serializers
from .models import Video, ViewRecord, ViewItem, View1, View2
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__'
    
    def create(self, validated_data):
        video = super().create(validated_data)
        cate = video.category
        video.category_big = cate
        if cate.pid:
            video.category_big = cate.pid
        video.save()
        View1.objects.get_or_create(video=video, defaults={'video': video})
        return video

class VideoUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Video
        fields = ['name', 'category', 'description', 'sort_str']
    
    def update(self, instance, validated_data):
        video = super().update(instance, validated_data)
        cate = video.category
        video.category_big = cate
        if cate.pid:
            video.category_big = cate.pid
        video.save()
        return video

class VideoListDetailSerializer(serializers.ModelSerializer):
    views_n = serializers.IntegerField(source='view1_video.views', read_only=True, label="总观看次数")
    viewsp_n = serializers.IntegerField(source='view1_video.viewsp', read_only=True, label="总观看秒数")
    class Meta:
        model = Video
        exclude = ['mediaurl']

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']


class ViewItemSerializer(serializers.ModelSerializer):
    create_by_ = UserSimpleSerializer(source='create_by', read_only=True)
    video_ = VideoSimpleSerializer(source='video', read_only=True)
    class Meta:
        model = ViewItem
        fields = '__all__'

class ViewItemUpdateSerializer(serializers.Serializer):
    current = serializers.IntegerField(min_value=1)
    seconds = serializers.IntegerField(min_value=0)


class View2Serializer(serializers.Serializer):
    create_by_ = UserSimpleSerializer(source='create_by', read_only=True)
    video_ = VideoSimpleSerializer(source='video', read_only=True)
    class Meta:
        model = View2
        fields = '__all__'

class DatetimeSerializer(serializers.Serializer):
    start_time = serializers.DateTimeField(label="开始时间")
    end_time = serializers.DateTimeField(label="结束时间")
    limit = serializers.IntegerField(min_value=10, label="展示数量")