"""
Notification serializers.
"""

from rest_framework import serializers

from .models import NotificationLog, NotificationPreference, Subscriber, SystemAnnouncement


class SubscriberSerializer(serializers.ModelSerializer):
    """Serializer for Subscriber model."""

    subscriber_id = serializers.CharField(source="novu_subscriber_id", read_only=True)

    class Meta:
        model = Subscriber
        fields = [
            "subscriber_id",
            "first_name",
            "last_name",
            "email",
            "phone",
            "avatar",
            "locale",
            "data",
            "created_at",
            "updated_at",
        ]
        read_only_fields = ["subscriber_id", "created_at", "updated_at"]


class CreateSubscriberSerializer(serializers.Serializer):
    """Serializer for creating a new subscriber."""

    subscriber_id = serializers.CharField(required=True)
    first_name = serializers.CharField(required=False, allow_blank=True, default="")
    last_name = serializers.CharField(required=False, allow_blank=True, default="")
    email = serializers.EmailField(required=False, allow_blank=True)
    phone = serializers.CharField(required=False, allow_blank=True, default="")
    avatar = serializers.URLField(required=False, allow_blank=True)
    locale = serializers.CharField(required=False, default="en")
    data = serializers.JSONField(required=False, default=dict)


class UpdateSubscriberSerializer(serializers.Serializer):
    """Serializer for updating a subscriber."""

    subscriber_id = serializers.CharField(required=True)
    first_name = serializers.CharField(required=False, allow_blank=True)
    last_name = serializers.CharField(required=False, allow_blank=True)
    email = serializers.EmailField(required=False, allow_blank=True)
    phone = serializers.CharField(required=False, allow_blank=True)
    avatar = serializers.URLField(required=False, allow_blank=True)
    locale = serializers.CharField(required=False)
    data = serializers.JSONField(required=False)


class NotificationPreferenceSerializer(serializers.ModelSerializer):
    """Serializer for NotificationPreference model."""

    class Meta:
        model = NotificationPreference
        fields = [
            "channel_preferences",
            "workflow_preferences",
            "frequency_settings",
            "is_opted_out",
            "updated_at",
        ]
        read_only_fields = ["updated_at"]


class UpdatePreferencesSerializer(serializers.Serializer):
    """Serializer for updating notification preferences."""

    user_id = serializers.CharField(required=True)
    preferences = serializers.JSONField(required=True)


class TriggerNotificationSerializer(serializers.Serializer):
    """Serializer for triggering a notification."""

    workflow_id = serializers.CharField(required=True)
    subscriber_id = serializers.CharField(required=True)
    payload = serializers.JSONField(required=False, default=dict)
    overrides = serializers.JSONField(required=False, default=dict)


class ModeratorSubmissionNotificationSerializer(serializers.Serializer):
    """Serializer for moderator submission notifications."""

    submission_id = serializers.CharField(required=True)
    submission_type = serializers.CharField(required=True)
    submitter_name = serializers.CharField(required=True)
    action = serializers.CharField(required=True)


class ModeratorReportNotificationSerializer(serializers.Serializer):
    """Serializer for moderator report notifications."""

    report_id = serializers.CharField(required=True)
    report_type = serializers.CharField(required=True)
    reported_entity_type = serializers.CharField(required=True)
    reported_entity_id = serializers.CharField(required=True)
    reporter_name = serializers.CharField(required=True)
    reason = serializers.CharField(required=True)
    entity_preview = serializers.CharField(required=False, allow_blank=True)
    reported_at = serializers.DateTimeField(required=False)


class SystemAnnouncementSerializer(serializers.ModelSerializer):
    """Serializer for system announcements."""

    class Meta:
        model = SystemAnnouncement
        fields = [
            "id",
            "title",
            "message",
            "severity",
            "action_url",
            "is_active",
            "created_at",
            "expires_at",
        ]
        read_only_fields = ["id", "created_at"]


class CreateAnnouncementSerializer(serializers.Serializer):
    """Serializer for creating system announcements."""

    title = serializers.CharField(required=True, max_length=255)
    message = serializers.CharField(required=True)
    severity = serializers.ChoiceField(
        choices=["info", "warning", "critical"],
        default="info",
    )
    action_url = serializers.URLField(required=False, allow_blank=True)


class NotificationLogSerializer(serializers.ModelSerializer):
    """Serializer for notification logs."""

    class Meta:
        model = NotificationLog
        fields = [
            "id",
            "workflow_id",
            "notification_type",
            "channel",
            "status",
            "payload",
            "error_message",
            "created_at",
        ]
        read_only_fields = ["id", "created_at"]
