package com.easyagents.document.pdf.mineru;

import com.easyagents.document.core.mineru.MineruClient;
import com.easyagents.document.core.mineru.MineruDocumentParseService;
import com.easyagents.document.core.mineru.MineruMapper;
import com.easyagents.document.core.mineru.MineruProperties;
import com.easyagents.document.core.entity.ParseRequest;
import com.easyagents.document.core.entity.PdfParseRequest;
import com.easyagents.document.pdf.PdfDocumentProvider;
import com.easyagents.core.util.StringUtil;

/**
 * 基于 MinerU API 的 PDF 解析服务。
 *
 * @author Codex
 * @since 2026-04-16
 */
public class MineruPdfDocumentParseService extends MineruDocumentParseService<PdfParseRequest> implements PdfDocumentProvider {

    public static final String PROVIDER_NAME = "mineru";

    /**
     * 创建默认服务实例。
     *
     * @param properties MinerU 配置
     */
    public MineruPdfDocumentParseService(MineruProperties properties) {
        super(properties);
    }

    /**
     * 创建默认服务实例。
     *
     * @param properties MinerU 配置
     * @param mapper 结果映射器
     */
    public MineruPdfDocumentParseService(MineruProperties properties, MineruMapper mapper) {
        super(properties, mapper);
    }

    /**
     * 创建服务实例。
     *
     * @param properties MinerU 配置
     * @param client HTTP 客户端
     * @param mapper 结果映射器
     */
    public MineruPdfDocumentParseService(MineruProperties properties, MineruClient client, MineruMapper mapper) {
        super(properties, client, mapper);
    }

    @Override
    public String getProvider() {
        return PROVIDER_NAME;
    }

    @Override
    protected ParseRequest normalizeRequest(ParseRequest request) {
        PdfParseRequest normalizedRequest = PdfParseRequest.from(request);
        ParseRequest commonRequest = super.normalizeRequest(normalizedRequest);
        commonRequest.copyCommonFieldsTo(normalizedRequest);
        normalizedRequest.setParseMethod(
            StringUtil.hasText(normalizedRequest.getParseMethod()) ? normalizedRequest.getParseMethod() : getProperties().getDefaultParseMethod()
        );
        normalizedRequest.setFormulaEnabled(
            normalizedRequest.getFormulaEnabled() == null ? getProperties().getDefaultFormulaEnable() : normalizedRequest.getFormulaEnabled()
        );
        normalizedRequest.setTableEnabled(
            normalizedRequest.getTableEnabled() == null ? getProperties().getDefaultTableEnable() : normalizedRequest.getTableEnabled()
        );
        normalizedRequest.setStartPageIndex(normalizedRequest.getStartPageIndex() == null ? 0 : normalizedRequest.getStartPageIndex());
        normalizedRequest.setEndPageIndex(normalizedRequest.getEndPageIndex() == null ? 99999 : normalizedRequest.getEndPageIndex());
        return normalizedRequest;
    }
}
