package com.easyagents.spring.boot.document.xlsx;

import com.easyagents.document.core.async.DocumentAsyncTaskManager;
import com.easyagents.document.core.async.InMemoryDocumentAsyncTaskRepository;
import com.easyagents.document.core.mineru.MineruProperties;
import com.easyagents.document.xlsx.XlsxDocumentParseService;
import com.easyagents.document.xlsx.mineru.MineruXlsxDocumentParseService;
import com.easyagents.spring.boot.document.mineru.CommonMineruDocumentProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * MinerU XLSX 自动装配。
 *
 * @author Codex
 * @since 2026-04-16
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MineruXlsxDocumentParseService.class)
@ConditionalOnProperty(prefix = "easy-agents.document.ocr", name = "provider", havingValue = "mineru")
@EnableConfigurationProperties(CommonMineruDocumentProperties.class)
public class MineruXlsxAutoConfiguration {

    public static final String XLSX_DOCUMENT_ASYNC_TASK_MANAGER_BEAN_NAME = "xlsxDocumentAsyncTaskManager";
    private static final int DEFAULT_ASYNC_THREADS = 2;

    @Bean
    @ConditionalOnMissingBean(name = XLSX_DOCUMENT_ASYNC_TASK_MANAGER_BEAN_NAME)
    public DocumentAsyncTaskManager xlsxDocumentAsyncTaskManager() {
        ExecutorService executorService = Executors.newFixedThreadPool(DEFAULT_ASYNC_THREADS);
        return new DocumentAsyncTaskManager(new InMemoryDocumentAsyncTaskRepository(), executorService);
    }

    @Bean
    @ConditionalOnMissingBean(XlsxDocumentParseService.class)
    public XlsxDocumentParseService xlsxDocumentParseService(CommonMineruDocumentProperties commonProperties,
                                                             @Qualifier(XLSX_DOCUMENT_ASYNC_TASK_MANAGER_BEAN_NAME)
                                                             DocumentAsyncTaskManager xlsxDocumentAsyncTaskManager) {
        return new MineruXlsxDocumentParseService(toMineruProperties(commonProperties), xlsxDocumentAsyncTaskManager);
    }

    private MineruProperties toMineruProperties(CommonMineruDocumentProperties properties) {
        MineruProperties mineruProperties = new MineruProperties();
        mineruProperties.setBaseUrl(properties.getBaseUrl());
        mineruProperties.setConnectTimeoutMs(properties.getConnectTimeoutMs());
        mineruProperties.setReadTimeoutMs(properties.getReadTimeoutMs());
        mineruProperties.setWriteTimeoutMs(properties.getWriteTimeoutMs());
        mineruProperties.setSubmitTimeoutMs(properties.getSubmitTimeoutMs());
        mineruProperties.setPollIntervalMs(properties.getPollIntervalMs());
        mineruProperties.setResultTimeoutMs(properties.getResultTimeoutMs());
        mineruProperties.setDefaultBackend(properties.getDefaultBackend());
        mineruProperties.setDefaultParseMethod(properties.getDefaultParseMethod());
        mineruProperties.setDefaultLangList(properties.getDefaultLangList());
        mineruProperties.setDefaultFormulaEnable(properties.getDefaultFormulaEnable());
        mineruProperties.setDefaultTableEnable(properties.getDefaultTableEnable());
        return mineruProperties;
    }
}
