"""
apps.core 全局 Snippet 的只读序列化器，供自定义业务接口
（/api/v1/custom/core/...）使用。图片字段统一序列化为
{"url": ..., "title": ...}，与前端 frontend/types/wagtail.ts
中既有的 background_image/logos 字段结构保持一致。
"""
from rest_framework import serializers


def serialize_image(image):
    if not image:
        return None
    return {"url": image.file.url, "title": image.title}


class TeamMemberSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    name = serializers.CharField()
    role = serializers.CharField()
    bio = serializers.CharField()
    photo = serializers.SerializerMethodField()

    def get_photo(self, obj):
        return serialize_image(obj.photo)


class TestimonialSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    quote = serializers.CharField()
    author_name = serializers.CharField()
    author_title = serializers.CharField()
    author_photo = serializers.SerializerMethodField()

    def get_author_photo(self, obj):
        return serialize_image(obj.author_photo)


class PartnerSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    name = serializers.CharField()
    website_url = serializers.CharField()
    logo = serializers.SerializerMethodField()

    def get_logo(self, obj):
        return serialize_image(obj.logo)


class NavigationMenuItemSerializer(serializers.Serializer):
    label = serializers.CharField()
    url = serializers.CharField()
    open_in_new_tab = serializers.BooleanField()


class NavigationMenuSerializer(serializers.Serializer):
    name = serializers.CharField()
    items = serializers.SerializerMethodField()

    def get_items(self, obj):
        return NavigationMenuItemSerializer(obj.items.all(), many=True).data


class SiteSettingsSerializer(serializers.Serializer):
    company_name = serializers.CharField()
    contact_phone = serializers.CharField()
    contact_email = serializers.CharField()
    address = serializers.CharField()
    icp_number = serializers.CharField()
    icp_url = serializers.CharField()
    wechat_account = serializers.CharField()
    weibo_url = serializers.CharField()
    wechat_qrcode = serializers.SerializerMethodField()

    def get_wechat_qrcode(self, obj):
        return serialize_image(obj.wechat_qrcode)
