package com.gzzm.lobster.parse;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Outline —— 整份文档的薄索引 / Thin outline of a parsed document.
 *
 * <p>序列化为 {@code <uuid>.outline.json} 落 ContentStore，并用 {@link #summarize}
 * 生成 workspace 索引段要展示的精简版。
 */
public class Outline {

    /** docx / xlsx / pptx / pdf / text */
    private String kind;
    private String title;
    private int totalChars;
    private List<OutlineSection> sections = new ArrayList<>();
    private Map<String, Object> stats = new LinkedHashMap<>();

    public Outline() {}

    public Outline(String kind, String title) {
        this.kind = kind;
        this.title = title;
    }

    /** 生成一份精简 summary，供 WorkspaceResource.metadataJson 冗余存储. */
    public Map<String, Object> summarize(int topN) {
        Map<String, Object> s = new LinkedHashMap<>();
        s.put("kind", kind);
        s.put("title", title);
        s.put("totalChars", totalChars);
        s.put("sectionCount", sections == null ? 0 : sections.size());
        if (stats != null && !stats.isEmpty()) s.put("stats", stats);
        List<String> top = new ArrayList<>();
        if (sections != null) {
            int take = Math.min(topN, sections.size());
            for (int i = 0; i < take; i++) {
                OutlineSection sec = sections.get(i);
                top.add(sec.getTitle() == null ? ("#" + sec.getId()) : sec.getTitle());
            }
        }
        s.put("topSections", top);
        return s;
    }

    public String getKind() { return kind; }
    public void setKind(String kind) { this.kind = kind; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public int getTotalChars() { return totalChars; }
    public void setTotalChars(int totalChars) { this.totalChars = totalChars; }
    public List<OutlineSection> getSections() { return sections; }
    public void setSections(List<OutlineSection> sections) { this.sections = sections; }
    public Map<String, Object> getStats() { return stats; }
    public void setStats(Map<String, Object> stats) { this.stats = stats; }
}
