from django import template
from django.core.serializers.json import DjangoJSONEncoder
import json

register = template.Library()


@register.filter
def serialize_photos(photos):
    """Serialize photos queryset to JSON for AlpineJS"""
    photo_data = []
    for photo in photos:
        photo_data.append(
            {
                "id": photo.id,
                "url": photo.image.url,
                "caption": photo.caption or "",
                "is_primary": photo.is_primary,
            }
        )
    return json.dumps(photo_data, cls=DjangoJSONEncoder)
