from rest_framework.fields import BooleanField, CharField, IntegerField, ListField
from rest_framework.serializers import Serializer


# Generic/common serializers
class ErrorResponseSerializer(Serializer):
    error = CharField(required=True)
    detail = CharField(required=False)


class PongResponseSerializer(Serializer):
    type = CharField(required=True)


# User message consumer
class UserMessageRequestSerializer(Serializer):
    text = CharField(required=True)


class UserMessageResponseSerializer(Serializer):
    ok = BooleanField(required=True)
    thread_id = CharField(required=True)
    message_id = CharField(required=True)


# Staff inbox consumer
class InboxThreadItemSerializer(Serializer):
    uuid = CharField(required=True)
    user_id = IntegerField(required=False, allow_null=True)
    email = CharField(required=True)
    assigned_to_id = IntegerField(required=False, allow_null=True)
    last_message_at = CharField(required=True)


class StaffInboxListResponseSerializer(Serializer):
    type = CharField(required=True)
    threads = ListField(child=InboxThreadItemSerializer())


class StaffAssignRequestSerializer(Serializer):
    action = CharField(required=True)
    thread_id = CharField(required=True)


class StaffAssignResponseSerializer(Serializer):
    type = CharField(required=True)
    thread_id = CharField(required=True)
    user = CharField(required=True)


class StaffReplyRequestSerializer(Serializer):
    action = CharField(required=True)
    thread_id = CharField(required=True)
    text = CharField(required=True)


class StaffReplyResponseSerializer(Serializer):
    type = CharField(required=True)
    message_id = CharField(required=True)


class StaffCloseRequestSerializer(Serializer):
    action = CharField(required=True)
    thread_id = CharField(required=True)


class StaffCloseResponseSerializer(Serializer):
    type = CharField(required=True)
    thread_id = CharField(required=True)


class StaffInboxEventSerializer(Serializer):
    """A flexible event serializer for staff inbox messages over WS."""

    type = CharField(required=True)
    # Optional fields depending on the event type
    threads = ListField(child=InboxThreadItemSerializer(), required=False)
    thread_id = CharField(required=False)
    user = CharField(required=False)
    message_id = CharField(required=False)
    error = CharField(required=False)
    detail = CharField(required=False)


# Thread consumer
class ThreadReplyRequestSerializer(Serializer):
    action = CharField(required=True)
    text = CharField(required=True)


class ThreadReplyResponseSerializer(Serializer):
    type = CharField(required=True)
    message_id = CharField(required=True)


class ThreadCloseRequestSerializer(Serializer):
    action = CharField(required=True)


class ThreadCloseResponseSerializer(Serializer):
    type = CharField(required=True)
    thread_id = CharField(required=True)


class ThreadOkResponseSerializer(Serializer):
    thread = CharField(required=True)
    ok = BooleanField(required=True)


class ThreadPongResponseSerializer(Serializer):
    type = CharField(required=True)
    thread = CharField(required=True)
