/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  <p>
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  <p>
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.easyagents.core.file2text.extractor;

import com.easyagents.core.file2text.DocumentReadSupport;
import com.easyagents.core.file2text.LightweightDocumentReadRequest;
import com.easyagents.core.file2text.LightweightDocumentReadResult;
import com.easyagents.core.file2text.source.DocumentSource;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;

/**
 * 文档文本提取器。
 */
public interface FileExtractor {

    Comparator<FileExtractor> ORDER_COMPARATOR =
        Comparator.comparingInt(FileExtractor::getOrder);

    /**
     * 判断该 Extractor 是否支持处理此文档。
     *
     * @param source 文档来源
     * @return 是否支持
     */
    boolean supports(DocumentSource source);

    /**
     * 提取兼容纯文本。
     *
     * @param source 文档来源
     * @return 提取文本
     * @throws IOException 文档读取失败
     */
    String extractText(DocumentSource source) throws IOException;

    /**
     * 提取结构化文档片段。
     *
     * @param request 读取请求
     * @return 结构化结果
     * @throws IOException 文档读取失败
     */
    default LightweightDocumentReadResult read(LightweightDocumentReadRequest request) throws IOException {
        String text = extractText(request.getSource());
        return DocumentReadSupport.result(request.getSource(),
                List.of(DocumentReadSupport.segment("segment-1", text, "DOCUMENT", "全文", 0, List.of())),
                request);
    }

    /**
     * 获取读取器优先级。
     *
     * @return 越小越优先
     */
    default int getOrder() {
        return 100;
    }
}
