from rest_framework import serializers

from accounts.models import Role

from .models import Notification, NotificationSetting, NotificationType


class NotificationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Notification
        fields = (
            'id', 'notification_type', 'title', 'message', 'link_path', 'is_read', 'created_at',
        )


class NotificationSettingSerializer(serializers.ModelSerializer):
    class Meta:
        model = NotificationSetting
        fields = ('id', 'role', 'notification_type', 'enabled')


class NotificationSettingUpdateSerializer(serializers.Serializer):
    role = serializers.CharField(max_length=50)
    notification_type = serializers.ChoiceField(choices=NotificationType.choices)
    enabled = serializers.BooleanField()

    def validate_role(self, value):
        if not Role.objects.filter(name=value).exists():
            raise serializers.ValidationError(f'No role named "{value}".')
        return value
