from rest_framework import serializers

from .models import Standard, UnitType, ImplementRule

from apps.system.serializers import DictSerializer


class StandardSerializer(serializers.ModelSerializer):
    class Meta:
        model = Standard
        fields = '__all__'

class ImplementRuleSerializer(serializers.ModelSerializer):
    cert_field_ = DictSerializer(source='cert_field', read_only=True)
    class Meta:
        model = ImplementRule
        fields = '__all__'

class ImplementRuleListSerializer(serializers.ModelSerializer):
    pv_scope = DictSerializer()
    pv_class = DictSerializer()
    cert_field = DictSerializer()
    ccc_list = DictSerializer(many=True)
    class Meta:
        model = ImplementRule
        fields = ['id', 'code', 'name', 'cert_field', 'pv_scope', 'pv_class', 'create_time', 'ccc_list']
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('pv_scope','pv_class', 'cert_field')
        queryset = queryset.prefetch_related('ccc_list',)
        return queryset
    
    # def get_cert_field(self, obj):
    #     return obj.get_cert_field_display()

class UnitTypeSerializer(serializers.ModelSerializer):
    standard_ = StandardSerializer(source='standard', read_only=True)
    class Meta:
        model = UnitType
        fields = '__all__'
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('standard','implementrule')
        return queryset