"""
故事模块序列化器
"""
from rest_framework import serializers
from .models import StoryShelf, Story


class StoryShelfSerializer(serializers.ModelSerializer):
    """书架序列化器"""
    story_count = serializers.IntegerField(read_only=True, default=0)

    class Meta:
        model = StoryShelf
        fields = ['id', 'name', 'capacity', 'is_locked', 'unlock_cost', 'story_count', 'created_at']
        read_only_fields = ['id', 'created_at']


class CreateShelfSerializer(serializers.Serializer):
    """创建书架序列化器"""
    name = serializers.CharField(max_length=100)


class StoryListSerializer(serializers.ModelSerializer):
    """故事列表序列化器"""

    class Meta:
        model = Story
        fields = ['id', 'title', 'cover_url', 'content', 'audio_url',
                  'has_video', 'video_url', 'created_at']


class StoryDetailSerializer(serializers.ModelSerializer):
    """故事详情序列化器"""

    class Meta:
        model = Story
        fields = ['id', 'title', 'content', 'cover_url', 'audio_url',
                  'has_video', 'video_url', 'generation_mode', 'prompt',
                  'shelf', 'created_at', 'updated_at']


class CreateStorySerializer(serializers.Serializer):
    """保存故事序列化器"""
    title = serializers.CharField(max_length=200)
    content = serializers.CharField()
    shelf_id = serializers.IntegerField()
    cover_url = serializers.URLField(required=False, allow_blank=True, default='')
    generation_mode = serializers.ChoiceField(
        choices=['ai', 'manual'], default='ai'
    )
    prompt = serializers.CharField(required=False, allow_blank=True, default='')


class GenerateStorySerializer(serializers.Serializer):
    """生成故事序列化器"""
    characters = serializers.ListField(
        child=serializers.CharField(), default=[]
    )
    scenes = serializers.ListField(
        child=serializers.CharField(), default=[]
    )
    props = serializers.ListField(
        child=serializers.CharField(), default=[]
    )
    shelf_id = serializers.IntegerField(required=False, allow_null=True)
