from apps.system.serializers import UserSimpleSerializer
import rest_framework
from rest_framework import serializers

from .models import State, Ticket, TicketFlow, Workflow, Transition, CustomField


class WorkflowSerializer(serializers.ModelSerializer):
    class Meta:
        model = Workflow
        fields = '__all__'

class StateSerializer(serializers.ModelSerializer):
    class Meta:
        model = State
        fields = '__all__'

class WorkflowSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Workflow
        fields = ['id', 'name']

class StateSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = State
        fields = ['id', 'name', 'type', 'distribute_type', 'enable_retreat']

class TransitionSerializer(serializers.ModelSerializer):
    source_state_ = StateSimpleSerializer(source='source_state', read_only=True)
    destination_state_ = StateSimpleSerializer(source='destination_state', read_only=True)
    class Meta:
        model = Transition
        fields = '__all__'
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('source_state','destination_state')
        return queryset


class CustomFieldSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomField
        fields = '__all__'

class TicketSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ticket
        fields = '__all__'

class TicketCreateSerializer(serializers.ModelSerializer):
    transition = serializers.IntegerField(label='流转ID')
    class Meta:
        model=Ticket
        fields=['title','workflow', 'ticket_data', 'transition']
    
    def create(self, validated_data):
        validated_data.pop('transition')
        return super().create(validated_data)

class TicketSerializer(serializers.ModelSerializer):
    workflow_ = WorkflowSimpleSerializer(source='workflow', read_only=True)
    state_ = StateSimpleSerializer(source='state', read_only=True)

    class Meta:
        model = Ticket
        fields = '__all__'
    
    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('workflow','state')
        return queryset

class TicketListSerializer(serializers.ModelSerializer):
    workflow_ = WorkflowSimpleSerializer(source='workflow', read_only=True)
    state_ = StateSimpleSerializer(source='state', read_only=True)

    class Meta:
        model = Ticket
        fields = ['id', 'title', 'sn', 'workflow', 'workflow_', 'state', 'state_', 'act_state', 'create_time', 'update_time', 'participant_type', 'create_by']
    
    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('workflow','state')
        return queryset

class TicketDetailSerializer(serializers.ModelSerializer):
    workflow_ = WorkflowSimpleSerializer(source='workflow', read_only=True)
    state_ = StateSimpleSerializer(source='state', read_only=True)
    ticket_data_ = serializers.SerializerMethodField()
    class Meta:
        model = Ticket
        fields = '__all__'
    
    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('workflow','state')
        return queryset

    def get_ticket_data_(self, obj):
        ticket_data = obj.ticket_data
        state_fields = obj.state.state_fields
        all_fields = CustomField.objects.filter(workflow=obj.workflow).order_by('sort')
        all_fields_l = CustomFieldSerializer(instance=all_fields, many=True).data
        for i in all_fields_l:
            key = i['field_key']
            i['field_state'] = state_fields.get(key, 1)
            i['field_value'] = ticket_data.get(key, None)
        return all_fields_l

class TicketFlowSerializer(serializers.ModelSerializer):
    participant_ = UserSimpleSerializer(source='participant', read_only=True)
    state_ = StateSimpleSerializer(source='state', read_only=True)
    class Meta:
        model = TicketFlow
        fields = '__all__'

class TicketFlowSimpleSerializer(serializers.ModelSerializer):
    participant_ = UserSimpleSerializer(source='participant', read_only=True)
    state_ = StateSimpleSerializer(source='state', read_only=True)
    class Meta:
        model = TicketFlow
        exclude = ['ticket_data']

    
class TicketHandleSerializer(serializers.Serializer):
    transition = serializers.IntegerField(label="流转id")
    ticket_data = serializers.JSONField(label="表单数据json")
    suggestion = serializers.CharField(label="处理意见", required = False)

class TicketRetreatSerializer(serializers.Serializer):
    suggestion = serializers.CharField(label="撤回原因", required = False)

class TicketCloseSerializer(serializers.Serializer):
    suggestion = serializers.CharField(label="关闭原因", required = False)

class TicketAddNodeSerializer(serializers.Serializer):
    suggestion = serializers.CharField(label="加签说明", required = False)
    toadd_user = serializers.IntegerField(label='发送给谁去加签')

class TicketAddNodeEndSerializer(serializers.Serializer):
    suggestion = serializers.CharField(label="加签意见", required = False)

class TicketDestorySerializer(serializers.Serializer):
    ids = serializers.ListField(child=serializers.IntegerField(), label='工单ID列表')