"""
Serializers for admin alert API endpoints.

Provides serializers for SystemAlert, RateLimitAlert, and RateLimitAlertConfig models.
"""

from rest_framework import serializers

from apps.core.models import RateLimitAlert, RateLimitAlertConfig, SystemAlert


class SystemAlertSerializer(serializers.ModelSerializer):
    """Serializer for system alerts."""

    is_resolved = serializers.BooleanField(read_only=True)
    resolved_by_username = serializers.CharField(source="resolved_by.username", read_only=True, allow_null=True)

    class Meta:
        model = SystemAlert
        fields = [
            "id",
            "alert_type",
            "severity",
            "message",
            "metadata",
            "resolved_at",
            "resolved_by",
            "resolved_by_username",
            "created_at",
            "is_resolved",
        ]
        read_only_fields = ["id", "created_at", "is_resolved", "resolved_by_username"]


class SystemAlertResolveSerializer(serializers.Serializer):
    """Serializer for resolving system alerts."""

    notes = serializers.CharField(required=False, allow_blank=True)


class RateLimitAlertConfigSerializer(serializers.ModelSerializer):
    """Serializer for rate limit alert configurations."""

    class Meta:
        model = RateLimitAlertConfig
        fields = [
            "id",
            "metric_type",
            "threshold_value",
            "time_window_ms",
            "function_name",
            "enabled",
            "created_at",
            "updated_at",
        ]
        read_only_fields = ["id", "created_at", "updated_at"]


class RateLimitAlertSerializer(serializers.ModelSerializer):
    """Serializer for rate limit alerts."""

    is_resolved = serializers.BooleanField(read_only=True)
    config_id = serializers.UUIDField(source="config.id", read_only=True)
    resolved_by_username = serializers.CharField(source="resolved_by.username", read_only=True, allow_null=True)

    class Meta:
        model = RateLimitAlert
        fields = [
            "id",
            "config_id",
            "metric_type",
            "metric_value",
            "threshold_value",
            "time_window_ms",
            "function_name",
            "alert_message",
            "resolved_at",
            "resolved_by",
            "resolved_by_username",
            "created_at",
            "is_resolved",
        ]
        read_only_fields = ["id", "created_at", "is_resolved", "config_id", "resolved_by_username"]


class RateLimitAlertResolveSerializer(serializers.Serializer):
    """Serializer for resolving rate limit alerts."""

    notes = serializers.CharField(required=False, allow_blank=True)
