from rest_framework import serializers
from .models import Notification


class NotificationSerializer(serializers.ModelSerializer):
    sender_username = serializers.SerializerMethodField()
    sender_avatar   = serializers.SerializerMethodField()

    class Meta:
        model  = Notification
        fields = [
            "id", "notif_type", "title", "body", "link",
            "is_read", "created_at",
            "sender_username", "sender_avatar",
        ]

    def get_sender_username(self, obj):
        return obj.sender.username if obj.sender else None

    def get_sender_avatar(self, obj):
        if not obj.sender or not obj.sender.profile_image:
            return None
        request = self.context.get("request")
        url = obj.sender.profile_image.url
        if request:
            return request.build_absolute_uri(url)
        return "http://127.0.0.1:8000" + url