from rest_framework import serializers
from .models import Competition, CompetitionEnrollment, ScoreDimension, Project, ProjectFile, Score, Comment
from shop.serializers import WeChatUserSerializer

class ScoreDimensionSerializer(serializers.ModelSerializer):
    class Meta:
        model = ScoreDimension
        fields = ['id', 'name', 'description', 'weight', 'max_score', 'order']

class CompetitionSerializer(serializers.ModelSerializer):
    score_dimensions = ScoreDimensionSerializer(many=True, read_only=True)
    display_cover_image = serializers.SerializerMethodField()
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    
    class Meta:
        model = Competition
        fields = ['id', 'title', 'description', 'rule_description', 'condition_description', 
                  'cover_image', 'cover_image_url', 'display_cover_image',
                  'start_time', 'end_time', 'status', 'project_visibility', 'status_display', 'is_active', 
                  'score_dimensions', 'created_at']

    def get_display_cover_image(self, obj):
        if obj.cover_image:
            return obj.cover_image.url
        return obj.cover_image_url

class CompetitionEnrollmentSerializer(serializers.ModelSerializer):
    user = WeChatUserSerializer(read_only=True)
    
    class Meta:
        model = CompetitionEnrollment
        fields = ['id', 'competition', 'user', 'role', 'status', 'created_at']
        read_only_fields = ['status']

class ProjectFileSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProjectFile
        fields = ['id', 'project', 'file_type', 'file', 'file_url', 'name', 'created_at']

    def validate_file(self, value):
        if not value:
            return value
        # 50MB limit
        limit_mb = 50
        if value.size > limit_mb * 1024 * 1024:
            raise serializers.ValidationError(f"文件大小不能超过 {limit_mb}MB")
        return value

class ProjectSerializer(serializers.ModelSerializer):
    files = ProjectFileSerializer(many=True, read_only=True)
    contestant_info = serializers.SerializerMethodField()
    display_cover_image = serializers.SerializerMethodField()
    
    class Meta:
        model = Project
        fields = ['id', 'competition', 'contestant', 'title', 'description', 'team_info', 
                  'cover_image', 'cover_image_url', 'display_cover_image',
                  'status', 'final_score', 'files', 'contestant_info', 'created_at']
        read_only_fields = ['final_score', 'contestant']

    def get_contestant_info(self, obj):
        return {
            "nickname": obj.contestant.user.nickname,
            "avatar_url": obj.contestant.user.avatar_url
        }

    def get_display_cover_image(self, obj):
        if obj.cover_image:
            return obj.cover_image.url
        return obj.cover_image_url

class ScoreSerializer(serializers.ModelSerializer):
    judge_name = serializers.CharField(source='judge.user.nickname', read_only=True)
    dimension_name = serializers.CharField(source='dimension.name', read_only=True)
    
    class Meta:
        model = Score
        fields = ['id', 'project', 'judge', 'dimension', 'score', 'judge_name', 'dimension_name', 'created_at']
        read_only_fields = ['judge']

class CommentSerializer(serializers.ModelSerializer):
    judge_name = serializers.CharField(source='judge.user.nickname', read_only=True)
    
    class Meta:
        model = Comment
        fields = ['id', 'project', 'judge', 'content', 'judge_name', 'created_at']
        read_only_fields = ['judge']
