from rest_framework import serializers
from .models import Chat, Message, MessageFile, MessageHistory, MessageReaction


class MessageFileSerializer(serializers.ModelSerializer):
    class Meta:
        model = MessageFile
        fields = ['id', 'file', 'media_type', 'uploaded_at']
        read_only_fields = ['uploaded_at']


class MessageReactionSerializer(serializers.ModelSerializer):
    class Meta:
        model = MessageReaction
        fields = ['id', 'user', 'emoji', 'created_at']
        read_only_fields = ['user', 'created_at']


class MessageHistorySerializer(serializers.ModelSerializer):
    class Meta:
        model = MessageHistory
        fields = ['id', 'old_content', 'archived_at']
        read_only_fields = ['archived_at']


class MessageSerializer(serializers.ModelSerializer):
    media_files = MessageFileSerializer(many=True, read_only=True)
    reactions = MessageReactionSerializer(many=True, read_only=True)

    class Meta:
        model = Message
        fields = [
            'id', 'chat', 'sender', 'reply_to',
            'content', 'is_edited', 'edited_at',
            'created_at', 'updated_at',
            'media_files', 'reactions',
        ]
        read_only_fields = ['sender', 'chat', 'reply_to', 'is_edited', 'edited_at', 'created_at', 'updated_at']


class MessageSendSerializer(serializers.Serializer):
    """Used for the HTTP send endpoint (text + optional files)."""
    chat = serializers.PrimaryKeyRelatedField(queryset=Chat.objects.all())
    content = serializers.CharField(required=False, allow_blank=True, default='')
    reply_to = serializers.PrimaryKeyRelatedField(
        queryset=Message.objects.all(), required=False, allow_null=True
    )
    # files come via request.FILES - declared here for schema documentation only
    files = serializers.ListField(
        child=serializers.FileField(allow_empty_file=False, use_url=False),
        required=False,
        default=list,
        write_only=True,
    )

    def validate(self, attrs):
        if not attrs.get('content') and not attrs.get('files'):
            raise serializers.ValidationError('A message must have content or at least one file.')
        return attrs


class ChatSerializer(serializers.ModelSerializer):
    class Meta:
        model = Chat
        fields = [
            'id', 'chat_type', 'owner', 'name',
            'icon', 'banner', 'members', 'moderators',
            'hub', 'created_at', 'updated_at',
        ]
        read_only_fields = ['owner', 'created_at', 'updated_at']


class ChatMemberSerializer(serializers.Serializer):
    user_id = serializers.IntegerField(help_text='PK of the user to add or remove.')
