"""
REST serializers for the Notification model.

No hardcoded URLs or project-specific fields.
"""
from __future__ import annotations

from django.utils.translation import get_language
from rest_framework import serializers

from django_notify_hub.models import Notification


class NotificationSerializer(serializers.ModelSerializer):
    """
    Read serializer for Notification objects.

    Adds ``title_display`` and ``body_display`` which return the localised
    string for the current request language (Accept-Language header).
    """

    title = serializers.SerializerMethodField()
    body = serializers.SerializerMethodField()
    data = serializers.SerializerMethodField()
    channel = serializers.SerializerMethodField()
    priority = serializers.SerializerMethodField()
    title_display = serializers.SerializerMethodField()
    body_display = serializers.SerializerMethodField()

    class Meta:
        model = Notification
        fields = [
            "id",
            "title",
            "body",
            "title_display",
            "body_display",
            "data",
            "channel",
            "priority",
            "status",
            "is_read",
            "created_at",
            "updated_at",
        ]
        read_only_fields = fields

    def get_title_display(self, obj: Notification) -> str:
        lang = get_language() or "en"
        return obj.get_title(lang)

    def get_title(self, obj: Notification):
        return obj.title

    def get_body(self, obj: Notification):
        return obj.body

    def get_data(self, obj: Notification):
        return obj.data

    def get_channel(self, obj: Notification) -> str:
        return obj.channel

    def get_priority(self, obj: Notification) -> str:
        return obj.priority

    def get_body_display(self, obj: Notification) -> str:
        lang = get_language() or "en"
        return obj.get_body(lang)


class MarkReadSerializer(serializers.Serializer):
    """Input serializer for bulk mark-as-read."""

    pks = serializers.ListField(
        child=serializers.IntegerField(min_value=1),
        min_length=1,
        help_text="List of notification PKs to mark as read.",
    )


class DeleteManySerializer(serializers.Serializer):
    """Input serializer for bulk delete."""

    pks = serializers.ListField(
        child=serializers.IntegerField(min_value=1),
        min_length=1,
        help_text="List of notification PKs to delete.",
    )

