from apps.plan.models import Plan
from rest_framework import serializers

from .models import *

from apps.system.serializers import DictSerializer, UserListSerializer
from apps.certset.serializers import StandardSerializer
from apps.plan.serializers import PlanSerializer

class ApplicationCreateSerializer(serializers.ModelSerializer):
    number = serializers.CharField(required=False)
    class Meta:
        model = Application
        fields = '__all__'

class ApplicationUpdateSerializer(serializers.ModelSerializer):
    number = serializers.CharField(read_only=True)
    stage_ = serializers.SerializerMethodField(source='stage', read_only=True)
    class Meta:
        model = Application
        fields = '__all__'

    def get_stage_(self, obj):
        return obj.get_stage_display()

class SubApplicationCreateSerializer(serializers.ModelSerializer):
    number = serializers.CharField(required=False)
    class Meta:
        model = SubApplication
        fields = '__all__'

class SubApplicationUpdateSerializer(serializers.ModelSerializer):
    number = serializers.CharField(read_only=True)
    cert_field_ = DictSerializer(source='cert_field')
    cccpv_class_ = DictSerializer(source='cccpv_class')
    cnas_scopes_ = DictSerializer(source='cnas_scopes', many=True)
    class Meta:
        model = SubApplication
        fields = '__all__'

class CertappSerializer(serializers.ModelSerializer):
    cert_field_ = DictSerializer(source='cert_field', read_only=True)
    cccpv_class_ = DictSerializer(source='cccpv_class' , read_only=True)
    cnas_scopes_ = DictSerializer(source='cnas_scopes', many=True , read_only=True)
    create_by_ = UserListSerializer(source='create_by', read_only=True)
    accept_by_ = UserListSerializer(source='accept_by', read_only=True)
    class Meta:
        model = CertApp
        fields = '__all__'

class EvaluationDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = EvaluationDetail
        fields = '__all__'


    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('cert_field', 'cccpv_class', 'create_by', 'accept_by')
        queryset = queryset.prefetch_related('cnas_scopes',)
        return queryset

class UnitSerializer(serializers.ModelSerializer):
    standard_ = StandardSerializer(source='standard', read_only=True)
    class Meta:
        model = Unit
        fields = '__all__'

class ProjectSerializer(serializers.ModelSerializer):
    create_by_ = UserListSerializer(source='create_by', read_only=True)
    assign_by_ = UserListSerializer(source='assign_by', read_only=True)
    certapps = serializers.SerializerMethodField()
    plan_ = PlanSerializer(source='plan', read_only=True)
    
    class Meta:
        model = Project
        fields = '__all__'
    
    def get_certapps(self, obj):
        certapps = []
        for i in obj.certapp_project.filter(is_deleted=False):
            certapps.append(i.cert_field.code +'(' +  i.cccpv_class.name +')')
        return certapps
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('create_by', 'assign_by', 'plan')
        queryset = queryset.prefetch_related('certapp_project')
        return queryset

class ProjectUpdateSerializer(serializers.ModelSerializer):

    class Meta:
        model = Project
        fields = ['remark', 'can_paichai']
