from rest_framework import serializers

from authentication.models import SystemUserProfile
from .models import (
    Ticket,
    TicketContent,
    Question,
    Answer,
    SupportUnit, TicketSupport, MessageSupport, TicketPermission, TicketClosePermission
)
from authentication.serializer.serializer import (
    SystemUserProfileSerializer,
    GroupSerializer, SystemUserProfileBaseInfoForTicketSerializer
)


class TicketContentSerializer(serializers.ModelSerializer):
    class Meta:
        model = TicketContent
        exclude = (
            'id',
            'modify_date',
            'created_by',
            'modified_by',
            'trash',
        )


class TicketQuestionSerializer(serializers.ModelSerializer):
    content = TicketContentSerializer(required=False)

    class Meta:
        model = Question
        fields = (
            'key',
            'content'
        )


class TicketAnswerSerializer(serializers.ModelSerializer):
    content = TicketContentSerializer(required=False)

    # question = TicketQuestionSerializer(required=False)

    class Meta:
        model = Answer
        fields = (
            'key',
            'content',
            # 'question'
        )


class SupportUnitSerializer(serializers.ModelSerializer):
    unit = GroupSerializer(required=False)

    class Meta:
        model = SupportUnit
        fields = (
            'unit',
        )


class TicketSerializer(serializers.ModelSerializer):
    customer = SystemUserProfileSerializer(required=False)
    operator = SystemUserProfileSerializer(required=False)
    support_unit = SupportUnitSerializer(required=False)
    # question = TicketQuestionSerializer(required=False, many=True)
    # respond = TicketAnswerSerializer(required=False, many=True)
    title = serializers.SerializerMethodField('get_title')
    data = serializers.SerializerMethodField('get_responds')

    class Meta:
        model = Ticket
        exclude = (
            'id',
            'created_by',
            'modified_by',
            'trash',
            'question',
            'respond',
        )

    def get_title(self, instance):
        title = instance.question.all()
        title = title[0].content.title
        return title

    def get_responds(self, instance):
        data = []
        for i in instance.question.all():
            question = TicketQuestionSerializer(i).data
            question['type'] = 'customer'
            data.append(question)
            for j in instance.respond.filter(question=i):
                answer = TicketAnswerSerializer(j).data
                answer['type'] = 'operator'
                data.append(answer)
        return data

    def update(self, instance, validated_data):
        instance.state = validated_data.get('state', instance.state)
        instance.save()
        return instance


class SystemUserProfileForTicketPermissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = SystemUserProfile
        fields=['key','fullname','mobile']


class TicketClosePermissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = TicketClosePermission
        fields = '__all__'


class TicketSupportSerializer(serializers.ModelSerializer):
    user = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    to_user = SystemUserProfileForTicketPermissionSerializer(read_only=True,many=True)
    referred_by = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    referred_to = SystemUserProfileForTicketPermissionSerializer(read_only=True,many=True)
    to_role = GroupSerializer(read_only=True,many=True)

    class Meta:
        model = TicketSupport
        fields = '__all__'


class MessageSupportSerializer(serializers.ModelSerializer):
    created_by = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    parent = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    read_by = SystemUserProfileForTicketPermissionSerializer(read_only=True,many=True)
    ticket= TicketSupportSerializer(read_only=True)

    class Meta:
        model = MessageSupport
        fields = '__all__'


class MessageSupportWithoutReadBySerializer(serializers.ModelSerializer):
    created_by = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    parent = SystemUserProfileForTicketPermissionSerializer(read_only=True)
    # read_by = SystemUserProfileForTicketPermissionSerializer(read_only=True,many=True)

    class Meta:
        model = MessageSupport
        exclude = ('read_by',)
        depth=1

class TicketPermissionSerializer(serializers.ModelSerializer):
    roles=serializers.SerializerMethodField()

    class Meta:
        model = TicketPermission
        fields = ['id','role','roles']

    def get_roles(self,obj):
        permissions = TicketPermission.objects.filter(role=obj.role).prefetch_related('roles')
        roles = [role.name for permission in permissions for role in permission.roles.all()]
        return roles

