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

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.pptx.PptxDocumentParseService;
import com.easyagents.document.pptx.mineru.MineruPptxDocumentParseService;
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 PPTX 自动装配。
 *
 * @author Codex
 * @since 2026-04-16
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MineruPptxDocumentParseService.class)
@ConditionalOnProperty(prefix = "easy-agents.document.ocr", name = "provider", havingValue = "mineru")
@EnableConfigurationProperties(CommonMineruDocumentProperties.class)
public class MineruPptxAutoConfiguration {

    public static final String PPTX_DOCUMENT_ASYNC_TASK_MANAGER_BEAN_NAME = "pptxDocumentAsyncTaskManager";
    private static final int DEFAULT_ASYNC_THREADS = 2;

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

    @Bean
    @ConditionalOnMissingBean(PptxDocumentParseService.class)
    public PptxDocumentParseService pptxDocumentParseService(CommonMineruDocumentProperties commonProperties,
                                                             @Qualifier(PPTX_DOCUMENT_ASYNC_TASK_MANAGER_BEAN_NAME)
                                                             DocumentAsyncTaskManager pptxDocumentAsyncTaskManager) {
        return new MineruPptxDocumentParseService(toMineruProperties(commonProperties), pptxDocumentAsyncTaskManager);
    }

    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;
    }
}
