from operator import truediv
from rest_framework import serializers
from .models import Candidate, Company, Consumer, PaySubject, ConsumerPerm, ConsumerRole, SendCode
from question.models import Question, Questioncat
from .permission import get_consumerperm_list
from rbac.serializers.user_serializer import UserListSerializer


class ConsumerPermSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConsumerPerm
        fields = '__all__'

class ConsumerRoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConsumerRole
        fields = '__all__'

class CompanySerializer(serializers.ModelSerializer):
    """
    公司序列化
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    create_admin_name = serializers.StringRelatedField(source='create_admin', read_only=True)
    class Meta:
        model = Company
        fields = '__all__'


class ConsumerSerializer(serializers.ModelSerializer):
    """
    客户序列化
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    company_name = serializers.StringRelatedField(source='company', read_only=True)
    company_ = CompanySerializer(source='company', read_only=True)
    subjects_name = serializers.StringRelatedField(source='subjects', many=True, read_only=True)
    workscope_name = serializers.StringRelatedField(source='workscope', read_only=True)
    role_name = serializers.StringRelatedField(source='role', read_only=True)
    create_admin_ = UserListSerializer(source='create_admin', read_only=True)
    create_admin_name = serializers.StringRelatedField(source='create_admin', read_only=True)
    class Meta:
        model = Consumer
        exclude = ('avatar','collects', 'process')
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('company','workscope', 'role', 'create_admin')
        queryset = queryset.prefetch_related('subjects',)
        return queryset

class ConsumerDetailSerializer(serializers.ModelSerializer):
    """
    客户详情序列化
    """
    company_name = serializers.StringRelatedField(source='company', read_only=True)
    subjects_name = serializers.StringRelatedField(source='subjects', many=True, read_only=True)
    workscope_name = serializers.StringRelatedField(source='workscope', read_only=True)
    role_name = serializers.StringRelatedField(source='role', read_only=True)
    perms = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = Consumer
        exclude = ('avatar','collects')
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('company','workscope', 'role')
        queryset = queryset.prefetch_related('subjects',)
        return queryset

    def get_perms(self,obj):
        return get_consumerperm_list(obj)


class CandidateSerializer(serializers.ModelSerializer):
    create_admin_username = serializers.StringRelatedField(source='create_admin', read_only=True)
    class Meta:
        model = Candidate
        fields = '__all__'
    
class CandidateCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Candidate
        exclude = ['consumer', 'examtest', 'workscope', 'is_manual', 'create_admin']

    def create(self, validated_data):
        validated_data['is_manual'] = True
        validated_data['create_admin'] = self.context['request'].user
        return super().create(validated_data)

class MsgSerializer(serializers.ModelSerializer):
    class Meta:
        model = SendCode
        fields='__all__'