/*
 * Copyright 2024-2026 the original author or authors.
 *
 * 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
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.alibaba.cloud.ai.agentic;

import com.alibaba.cloud.ai.agent.studio.loader.AgentLoader;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.graph.GraphRepresentation;
import com.alibaba.cloud.ai.graph.agent.BaseAgent;
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.checkpoint.savers.MemorySaver;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbackProvider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;

import jakarta.annotation.Nonnull;


/**
 * Static Agent Loader for programmatically provided agents.
 *
 * <p>This loader takes a static list of pre-created agent instances and makes them available
 * through the AgentLoader interface. Perfect for cases where you already have agent instances and
 * just need a convenient way to wrap them in an AgentLoader.
 *
 * <p>Works with or without MCP: when {@link ToolCallbackProvider} is absent (e.g. graph-only profile),
 * only {@code single_agent} (with PoetTool) is registered; when present, both {@code single_agent}
 * and {@code research_agent} are available.
 */
@Component
public class AgentStaticLoader implements AgentLoader {

	private final Map<String, BaseAgent> agents = new ConcurrentHashMap<>();

	public AgentStaticLoader(@Autowired(required = false) ToolCallbackProvider toolCallbackProvider) {
		// Create DashScopeApi instance using the API key from environment variable
		DashScopeApi dashScopeApi = DashScopeApi.builder().apiKey(System.getenv("AI_DASHSCOPE_API_KEY")).build();
		// Create DashScope ChatModel instance
		ChatModel chatModel = DashScopeChatModel.builder().dashScopeApi(dashScopeApi).build();

		ReactAgent agent = ReactAgent.builder()
				.name("single_agent")
				.model(chatModel)
				.saver(new MemorySaver())
				.tools(PoetTool.createPoetToolCallback())
				.build();

		this.agents.put("single_agent", agent);

		List<ToolCallback> toolCallbacks = toolCallbackProvider != null
				? new ArrayList<>(List.of(toolCallbackProvider.getToolCallbacks()))
				: Collections.emptyList();

		if (!toolCallbacks.isEmpty()) {
			ReactAgent researchAgent = new DeepResearchAgent().getResearchAgent(toolCallbacks);
			GraphRepresentation representation = researchAgent.getAndCompileGraph().stateGraph.getGraph(GraphRepresentation.Type.PLANTUML);
			this.agents.put("research_agent", researchAgent);
		}
	}

	@Override
	@Nonnull
	public List<String> listAgents() {
		return agents.keySet().stream().toList();
	}

	@Override
	public BaseAgent loadAgent(String name) {
		if (name == null || name.trim().isEmpty()) {
			throw new IllegalArgumentException("Agent name cannot be null or empty");
		}

		BaseAgent agent = agents.get(name);
		if (agent == null) {
			throw new NoSuchElementException("Agent not found: " + name);
		}

		return agent;
	}
}
