"""
REST API serializers for Themis.
"""
from rest_framework import serializers

from themis.encryption import encrypt_value
from themis.models import UserAPIKey, UserNotification, UserProfile


class UserProfileSerializer(serializers.ModelSerializer):
    """Serializer for UserProfile model."""

    username = serializers.CharField(source="user.username", read_only=True)
    email = serializers.EmailField(source="user.email", read_only=True)

    class Meta:
        model = UserProfile
        fields = [
            "id",
            "username",
            "email",
            "home_timezone",
            "current_timezone",
            "effective_timezone",
            "is_traveling",
            "date_format",
            "time_format",
            "thousand_separator",
            "week_start",
            "theme_mode",
            "theme_name",
            "dark_theme_name",
            "notifications_enabled",
            "notifications_min_level",
            "browser_notifications_enabled",
            "notification_retention_days",
            "created_at",
            "updated_at",
        ]
        read_only_fields = [
            "id",
            "effective_timezone",
            "is_traveling",
            "created_at",
            "updated_at",
        ]


class UserAPIKeyListSerializer(serializers.ModelSerializer):
    """Serializer for listing API keys (no encrypted values)."""

    masked_value = serializers.CharField(read_only=True)
    display_name = serializers.CharField(read_only=True)

    class Meta:
        model = UserAPIKey
        fields = [
            "id",
            "service_name",
            "key_type",
            "label",
            "display_name",
            "masked_value",
            "instructions",
            "help_url",
            "is_active",
            "last_used_at",
            "expires_at",
            "created_at",
            "updated_at",
        ]
        read_only_fields = [
            "id",
            "masked_value",
            "display_name",
            "created_at",
            "updated_at",
        ]


class UserAPIKeyCreateSerializer(serializers.Serializer):
    """Serializer for creating a new API key."""

    service_name = serializers.CharField(max_length=100)
    key_type = serializers.ChoiceField(choices=UserAPIKey.KEY_TYPE_CHOICES)
    label = serializers.CharField(max_length=100, required=False, default="")
    key_value = serializers.CharField(write_only=True)
    instructions = serializers.CharField(required=False, default="")
    help_url = serializers.URLField(required=False, default="")

    def create(self, validated_data):
        """Create API key with encrypted value."""
        key_value = validated_data.pop("key_value")
        validated_data["encrypted_value"] = encrypt_value(key_value)
        validated_data["user"] = self.context["request"].user
        return UserAPIKey.objects.create(**validated_data)


class UserNotificationSerializer(serializers.ModelSerializer):
    """Serializer for UserNotification model."""

    level_css_class = serializers.CharField(read_only=True)
    level_badge_class = serializers.CharField(read_only=True)

    class Meta:
        model = UserNotification
        fields = [
            "id",
            "title",
            "message",
            "level",
            "level_css_class",
            "level_badge_class",
            "url",
            "source_app",
            "source_model",
            "source_id",
            "is_read",
            "read_at",
            "is_dismissed",
            "dismissed_at",
            "expires_at",
            "created_at",
            "updated_at",
        ]
        read_only_fields = [
            "id",
            "title",
            "message",
            "level",
            "level_css_class",
            "level_badge_class",
            "url",
            "source_app",
            "source_model",
            "source_id",
            "read_at",
            "dismissed_at",
            "expires_at",
            "created_at",
            "updated_at",
        ]
