/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  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
 *  <p>
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  <p>
 *  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.easyagents.core.agent.route;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Agent 注册中心，用于管理所有可用的 ReActAgent 工厂。
 */
public class RoutingAgentRegistry {

    private final Map<String, RoutingAgentFactory> agentFactories = new HashMap<>();
    private final Map<String, String> agentDescriptions = new HashMap<>();
    private final Map<String, String> keywordToAgent = new HashMap<>();

    /**
     * 注册 Agent，并可选绑定关键字（用于快速匹配）
     */
    public void register(String name, String description, RoutingAgentFactory factory) {
        register(name, description, null, factory);
    }

    public void register(String name, String description, List<String> keywords, RoutingAgentFactory factory) {
        agentFactories.put(name, factory);
        agentDescriptions.put(name, description);

        if (keywords != null && !keywords.isEmpty()) {
            for (String kw : keywords) {
                if (kw != null && !kw.trim().isEmpty()) {
                    keywordToAgent.put(kw.trim().toLowerCase(), name);
                }
            }
        }
    }

    // 按关键字查找 Agent
    public String findAgentByKeyword(String userQuery) {
        if (userQuery == null) return null;
        String lowerQuery = userQuery.toLowerCase();
        for (Map.Entry<String, String> entry : keywordToAgent.entrySet()) {
            if (lowerQuery.contains(entry.getKey())) {
                return entry.getValue();
            }
        }
        return null;
    }


    public RoutingAgentFactory getAgentFactory(String name) {
        return agentFactories.get(name);
    }

    public String getAgentDescriptions() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : agentDescriptions.entrySet()) {
            sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
        }
        return sb.toString().trim();
    }
}
