from rest_framework import serializers
from .models import (
    Category, Project, Coupon, Banner, Student, StudentCoupon,
    Activity, Showcase, Exam, StudentHonor, StudentLevel,
    CourseRegistration, ActivityRegistration, ExamRegistration
)

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'

class ProjectSerializer(serializers.ModelSerializer):
    category_name = serializers.CharField(source='category.name', read_only=True)
    category_color = serializers.CharField(source='category.color', read_only=True)

    class Meta:
        model = Project
        fields = '__all__'

class CouponSerializer(serializers.ModelSerializer):
    scope_text = serializers.SerializerMethodField()
    applicable_project_titles = serializers.SerializerMethodField()

    class Meta:
        model = Coupon
        fields = '__all__'

    def get_scope_text(self, obj):
        ids = list(obj.applicable_projects.values_list('id', flat=True))
        if not ids:
            return '通用'
        names = list(obj.applicable_projects.values_list('title', flat=True))
        return '，'.join(names)

    def get_applicable_project_titles(self, obj):
        return list(obj.applicable_projects.values_list('title', flat=True))

class StudentCouponSerializer(serializers.ModelSerializer):
    coupon_detail = CouponSerializer(source='coupon', read_only=True)

    class Meta:
        model = StudentCoupon
        fields = '__all__'

class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = '__all__'

class StudentSerializer(serializers.ModelSerializer):
    stats = serializers.SerializerMethodField()
    coupons = serializers.PrimaryKeyRelatedField(many=True, read_only=True, source='student_coupons')

    class Meta:
        model = Student
        fields = ['id', 'name', 'phone', 'age', 'education', 'address', 'avatar', 'stats', 'coupons', 'created_at']
        read_only_fields = ['stats', 'coupons']

    def get_stats(self, obj):
        return {
            "learning": obj.learning_count,
            "coupons": obj.coupon_count,
            "hours": obj.study_hours
        }

class ActivitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Activity
        fields = '__all__'

class ShowcaseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Showcase
        fields = '__all__'

class ExamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Exam
        fields = '__all__'

class StudentHonorSerializer(serializers.ModelSerializer):
    class Meta:
        model = StudentHonor
        fields = '__all__'

class StudentLevelSerializer(serializers.ModelSerializer):
    exam_title = serializers.CharField(source='exam.title', read_only=True)

    class Meta:
        model = StudentLevel
        fields = '__all__'

class CourseRegistrationSerializer(serializers.ModelSerializer):
    project_detail = ProjectSerializer(source='project', read_only=True)
    class Meta:
        model = CourseRegistration
        fields = '__all__'

class ActivityRegistrationSerializer(serializers.ModelSerializer):
    activity_detail = ActivitySerializer(source='activity', read_only=True)
    class Meta:
        model = ActivityRegistration
        fields = '__all__'

class ExamRegistrationSerializer(serializers.ModelSerializer):
    exam_detail = ExamSerializer(source='exam', read_only=True)
    class Meta:
        model = ExamRegistration
        fields = '__all__'
