/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0.
 */
package com.easyagents.core.file2text;

import java.util.ArrayList;
import java.util.List;

/**
 * 轻量文档结构化读取结果。
 */
public class LightweightDocumentReadResult {

    /** 当前读取器版本。 */
    public static final String READER_VERSION = "v1";
    /** 当前读取策略版本。 */
    public static final String READ_POLICY_VERSION = "v1";

    private String fileName;
    private String mimeType;
    private String readerVersion = READER_VERSION;
    private String readPolicyVersion = READ_POLICY_VERSION;
    private int charCount;
    private int tokenEstimate;
    private List<DocumentTextSegment> segments = new ArrayList<>();

    /** @return 文件名 */
    public String getFileName() { return fileName; }

    /** @param fileName 文件名 */
    public void setFileName(String fileName) { this.fileName = fileName; }

    /** @return MIME 类型 */
    public String getMimeType() { return mimeType; }

    /** @param mimeType MIME 类型 */
    public void setMimeType(String mimeType) { this.mimeType = mimeType; }

    /** @return 读取器版本 */
    public String getReaderVersion() { return readerVersion; }

    /** @param readerVersion 读取器版本 */
    public void setReaderVersion(String readerVersion) { this.readerVersion = readerVersion; }

    /** @return 读取策略版本 */
    public String getReadPolicyVersion() { return readPolicyVersion; }

    /** @param readPolicyVersion 读取策略版本 */
    public void setReadPolicyVersion(String readPolicyVersion) { this.readPolicyVersion = readPolicyVersion; }

    /** @return 字符数 */
    public int getCharCount() { return charCount; }

    /** @param charCount 字符数 */
    public void setCharCount(int charCount) { this.charCount = charCount; }

    /** @return Token 估算 */
    public int getTokenEstimate() { return tokenEstimate; }

    /** @param tokenEstimate Token 估算 */
    public void setTokenEstimate(int tokenEstimate) { this.tokenEstimate = tokenEstimate; }

    /** @return 文本片段 */
    public List<DocumentTextSegment> getSegments() { return segments; }

    /** @param segments 文本片段 */
    public void setSegments(List<DocumentTextSegment> segments) {
        this.segments = segments == null ? new ArrayList<>() : new ArrayList<>(segments);
    }

    /**
     * 按片段顺序拼接兼容纯文本。
     *
     * @return 拼接后的文本
     */
    public String getText() {
        StringBuilder text = new StringBuilder(Math.max(0, charCount));
        for (DocumentTextSegment segment : segments) {
            if (segment.getText() == null || segment.getText().isBlank()) {
                continue;
            }
            if (!text.isEmpty()) {
                text.append('\n');
            }
            text.append(segment.getText());
        }
        return text.toString();
    }
}
