package com.lingyue.document.dto;

import com.lingyue.document.entity.DocumentBlock.TextElement;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Date;

/**
 * 结构化文档 DTO（参考飞书设计）
 * 包含文档的完整结构化内容，用于编辑器渲染
 * 
 * 核心设计：
 * - 文档由 Block 树组成
 * - 每个 Block 包含 elements 数组（TextElement）
 * - 实体作为 TextElement 嵌入，无需字符偏移定位
 * 
 * @author lingyue
 * @since 2026-01-21
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "结构化文档")
public class StructuredDocumentDTO {
    
    @Schema(description = "文档ID")
    private String documentId;
    
    @Schema(description = "版本号")
    private Integer revision;
    
    @Schema(description = "文档标题")
    private String title;
    
    @Schema(description = "文档状态")
    private String status;
    
    @Schema(description = "内容块列表")
    private List<BlockDTO> blocks;
    
    @Schema(description = "实体统计")
    private EntityStats entityStats;
    
    @Schema(description = "最后更新时间")
    private Date updatedAt;
    
    /**
     * 内容块 DTO（参考飞书Block）
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "内容块")
    public static class BlockDTO {
        
        @Schema(description = "块ID")
        private String id;
        
        @Schema(description = "父块ID")
        private String parentId;
        
        @Schema(description = "子块ID列表")
        private List<String> children;
        
        @Schema(description = "块序号")
        private Integer index;
        
        @Schema(description = "块类型", example = "page/heading1/heading2/text/bullet/ordered/table/image/code/quote")
        private String type;
        
        @Schema(description = "块内元素列表（核心：实体作为元素嵌入）")
        private List<TextElement> elements;
        
        @Schema(description = "纯文本内容（便于搜索）")
        private String plainText;
        
        @Schema(description = "HTML渲染（原文视图）")
        private String html;
        
        @Schema(description = "HTML渲染（标记视图，实体高亮）")
        private String markedHtml;
    }
    
    /**
     * 实体统计
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "实体统计")
    public static class EntityStats {
        
        @Schema(description = "总实体数")
        private Integer total;
        
        @Schema(description = "已确认数")
        private Integer confirmed;
        
        @Schema(description = "按类型统计")
        private java.util.Map<String, Integer> byType;
    }
}
