from rest_framework import serializers
from .models import mindMap, Node


class DocNodeSerializer(serializers.Serializer):
    id = serializers.CharField()
    isRoot = serializers.BooleanField()
    parentId = serializers.CharField(allow_null=True)
    childrenCount = serializers.IntegerField()
    depth = serializers.IntegerField()
    title = serializers.CharField(allow_blank=True, required=False)
    des = serializers.CharField(allow_blank=True, required=False)
    htmlContent = serializers.CharField(allow_blank=True, required=False)
    createDate = serializers.DateTimeField()
    updateDate = serializers.DateTimeField()
    delete = serializers.BooleanField()


class DocMindmapSerializer(serializers.Serializer):
    id = serializers.CharField()
    title = serializers.CharField()
    nodes = DocNodeSerializer(many=True)
    createDate = serializers.DateTimeField()
    updateDate = serializers.DateTimeField()
    delete = serializers.BooleanField()


def map_node_to_doc(node: Node) -> dict:
    return {
        'id': str(node.id),
        'isRoot': bool(node.is_root),
        'parentId': node.parent_id,
        'childrenCount': int(node.children_count or 0),
        'depth': int(node.depth or 0),
        'title': node.title or '',
        'des': (node.desc or ''),
        'htmlContent': node.html_content or '',
        'createDate': node.created_at,
        'updateDate': node.updated_at,
        'delete': bool(node.deleted),
    }


def map_mindmap_to_doc(m: mindMap, nodes: list[Node]) -> dict:
    return {
        'id': str(m.id),
        'title': m.file_name or '思维导图',
        'nodes': [map_node_to_doc(n) for n in nodes],
        'createDate': m.created_at,
        'updateDate': m.updated_at,
        'delete': bool(m.deleted),
    }
