package org.dromara.ai.config;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.ai.provider.AiProvider;
import org.dromara.ai.provider.AiProviderManager;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class AiProviderRegistrar {

    private final AiProviderManager providerManager;
    private final List<AiProvider> providers;
    private final org.dromara.ai.mcp.McpToolRegistry registry;

    @PostConstruct
    public void register() {
        for (AiProvider provider : providers) {
            try {
                provider.initialize(Collections.emptyMap());
                providerManager.registerProvider(provider);
                log.info("已注册AI提供商: {}", provider.getProviderName());
            } catch (Exception e) {
                log.error("注册AI提供商失败: {}", provider.getProviderName(), e);
            }
        }
        try {
            registry.register(new org.dromara.ai.mcp.tools.ProjectScaffoldTool());
            registry.register(new org.dromara.ai.mcp.tools.DataTransformTool());
            registry.register(new org.dromara.ai.mcp.tools.ApiCallTool());
            registry.register(new org.dromara.ai.mcp.tools.AlgorithmComputeTool());
            registry.register(new org.dromara.ai.mcp.tools.FileParseTool());
            registry.register(new org.dromara.ai.mcp.tools.HttpFetchTool());
        } catch (Exception e) {
            log.error("注册MCP工具失败", e);
        }
    }
}
