package tech.easyflow.agent.config;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Agent 五类产品级内置工具的类型化配置。
 */
public final class AgentBuiltinToolsConfig {

    /** 当前配置结构版本。 */
    public static final int SCHEMA_VERSION = 1;

    private final ToolSwitch read;
    private final ToolSwitch write;
    private final ToolSwitch patch;
    private final ToolSwitch shell;
    private final ToolSwitch artifactPublish;

    /**
     * 创建内置工具配置。
     *
     * @param read 读取工具配置
     * @param write 写入工具配置
     * @param patch 补丁工具配置
     * @param shell Shell 工具配置
     * @param artifactPublish 产物发布工具配置
     */
    public AgentBuiltinToolsConfig(ToolSwitch read,
                                   ToolSwitch write,
                                   ToolSwitch patch,
                                   ToolSwitch shell,
                                   ToolSwitch artifactPublish) {
        this.read = read;
        this.write = write;
        this.patch = patch;
        this.shell = shell;
        this.artifactPublish = artifactPublish;
    }

    /**
     * 返回新 Agent 的安全默认配置。
     *
     * @return 五项启用且仅 Shell 要求审批的配置
     */
    public static AgentBuiltinToolsConfig newAgentDefaults() {
        return new AgentBuiltinToolsConfig(
                new ToolSwitch(true, false),
                new ToolSwitch(true, false),
                new ToolSwitch(true, false),
                new ToolSwitch(true, true),
                new ToolSwitch(true, false));
    }

    /**
     * 返回旧发布快照的无扩权兼容配置。
     *
     * @return 五项全部禁用的配置
     */
    public static AgentBuiltinToolsConfig allDisabled() {
        ToolSwitch disabled = new ToolSwitch(false, false);
        return new AgentBuiltinToolsConfig(disabled, disabled, disabled, disabled, disabled);
    }

    /** @return 读取工具配置 */
    public ToolSwitch read() { return read; }
    /** @return 写入工具配置 */
    public ToolSwitch write() { return write; }
    /** @return 补丁工具配置 */
    public ToolSwitch patch() { return patch; }
    /** @return Shell 工具配置 */
    public ToolSwitch shell() { return shell; }
    /** @return 产物发布工具配置 */
    public ToolSwitch artifactPublish() { return artifactPublish; }

    /**
     * 转换为可写入 executionConfigJson 的稳定结构。
     *
     * @return 不含权限确认临时字段的安全 Map
     */
    public Map<String, Object> toMap() {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("schemaVersion", SCHEMA_VERSION);
        result.put("read", read.toMap());
        result.put("write", write.toMap());
        result.put("patch", patch.toMap());
        result.put("shell", shell.toMap());
        result.put("artifactPublish", artifactPublish.toMap());
        return result;
    }

    /**
     * 单个内置工具的启用与审批配置。
     *
     * @param enabled 是否启用
     * @param approvalRequired 是否要求调用前审批
     */
    public record ToolSwitch(boolean enabled, boolean approvalRequired) {

        /**
         * 转换为持久化结构。
         *
         * @return 工具开关 Map
         */
        public Map<String, Object> toMap() {
            Map<String, Object> result = new LinkedHashMap<>();
            result.put("enabled", enabled);
            result.put("approvalRequired", approvalRequired);
            return result;
        }
    }
}
