from rest_framework import serializers
from .models import Food, UserFood, FoodUsageLog


class FoodSerializer(serializers.ModelSerializer):
    """食物序列化器"""
    
    taste_tags_list = serializers.ReadOnlyField(source='get_taste_tags_list')
    is_available = serializers.ReadOnlyField()
    rarity_display = serializers.ReadOnlyField(source='get_rarity_display')
    food_type_display = serializers.ReadOnlyField(source='get_food_type_display')
    status_display = serializers.ReadOnlyField(source='get_status_display')
    
    class Meta:
        model = Food
        fields = [
            'id', 'name', 'food_type', 'food_type_display', 'description',
            'rarity', 'rarity_display', 'image', 'animation_file', 'sound_effect',
            'calories', 'taste_tags', 'taste_tags_list', 'cooking_methods',
            'nutritional_value', 'effect_description', 'boost_attributes',
            'unlock_condition', 'usage_limit', 'status', 'status_display',
            'is_limited', 'available_from', 'available_until', 'is_available',
            'created_at', 'updated_at', 'published_at'
        ]
        read_only_fields = ['created_at', 'updated_at', 'published_at']


class FoodListSerializer(serializers.ModelSerializer):
    """食物列表序列化器（简化版）"""
    
    rarity_display = serializers.ReadOnlyField(source='get_rarity_display')
    food_type_display = serializers.ReadOnlyField(source='get_food_type_display')
    is_available = serializers.ReadOnlyField()
    
    status_display = serializers.ReadOnlyField(source='get_status_display')

    class Meta:
        model = Food
        fields = [
            'id', 'name', 'food_type', 'food_type_display', 'rarity',
            'rarity_display', 'image', 'calories', 'is_available',
            'is_limited', 'status', 'status_display', 'published_at',
            'created_at'
        ]


class UserFoodSerializer(serializers.ModelSerializer):
    """用户食物序列化器"""
    
    food = FoodListSerializer(read_only=True)
    can_use = serializers.ReadOnlyField()
    user_name = serializers.ReadOnlyField(source='user.username')
    
    class Meta:
        model = UserFood
        fields = [
            'id', 'user', 'user_name', 'food', 'quantity', 'used_count',
            'obtained_at', 'obtained_method', 'last_used_at', 'can_use'
        ]
        read_only_fields = ['obtained_at', 'user']


class FoodUsageLogSerializer(serializers.ModelSerializer):
    """食物使用记录序列化器"""
    
    food_name = serializers.ReadOnlyField(source='food.name')
    user_name = serializers.ReadOnlyField(source='user.username')
    
    class Meta:
        model = FoodUsageLog
        fields = [
            'id', 'user', 'user_name', 'food', 'food_name', 'used_at',
            'effect_applied', 'notes', 'usage_context'
        ]
        read_only_fields = ['used_at', 'user']


class UseFoodSerializer(serializers.Serializer):
    """使用食物序列化器"""
    
    food_id = serializers.IntegerField()
    usage_context = serializers.CharField(max_length=100, required=False, allow_blank=True)
    notes = serializers.CharField(required=False, allow_blank=True)
    
    def validate_food_id(self, value):
        """验证食物ID"""
        try:
            food = Food.objects.get(id=value)
            if not food.is_available():
                raise serializers.ValidationError("该食物当前不可用")
            return value
        except Food.DoesNotExist:
            raise serializers.ValidationError("食物不存在")