package cn.com.poc.thirdparty.resource.demand.ai.function.long_memory;

import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.DateUtils;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfig;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 长期记忆
 */

@Component
public class SetLongMemoryFunction extends AbstractLargeModelFunction {

    private final String desc = "该方法仅用来保存用户想记录的内容，不能通过该方法进行查询。";

    private final FunctionLLMConfig functionLLMConfig = new FunctionLLMConfig.FunctionLLMConfigBuilder()
            .name("set_long_memory")
            .description(desc)
            .parameters(new Parameters("object").addProperties("content", new Properties("string", "内容的详细说明")))
            .build();

//    private final Long expireTime = 30 * 60 * 24L; // 30天有效期


    @Resource
    private RedisService redisService;

    @Override
    public String getDesc() {
        return desc;
    }

    /**
     * 执行函数
     *
     * @param content 内容
     * @param key     唯一标识
     * @return
     */
    @Override
    public String doFunction(String content, String key) {
        // 创建 JSONObject 对象
        JSONObject jsonObject = new JSONObject(content);
        // 提取 content
        String contents = jsonObject.getStr("content");
        String contentKey = SetLongMemoryConstants.REDIS_PREFIX + key;
        redisService.hset(contentKey, DateUtils.getCurrTime(), contents);
        return "SUCCESS";
    }

    @Override
    public List<String> getLLMConfig() {
//        Map<String, Object> config = new HashMap<>();
//        Map<String, Object> function = new HashMap<>();
//
//        Map<String, Object> content = new HashMap<>();
//        content.put("type", "string");
//        content.put("description", "内容的详细说明");
//
//        Map<String, Object> properties = new HashMap<>();
//        properties.put("content", content);
//
//        Map<String, Object> parameters = new HashMap<>();
//        parameters.put("type", "object");
//        parameters.put("properties", properties);
//
//        List<String> required = new ArrayList<>();
//        required.add("content");
//        parameters.put("required", required);
//
//
//        function.put("name", "set_long_memory");
//        function.put("description", desc);
//        function.put("parameters", parameters);
//
//        config.put("type", "function");
//        config.put("function", function);
//
//        // 将 Map 转换为 JSON 字符串
//        Gson gson = new Gson();
//        String jsonString = gson.toJson(config);
//
//        List<String> resultList = new ArrayList<>();
//        resultList.add(jsonString);
//        return resultList;
        return ListUtil.toList(JsonUtils.serialize(functionLLMConfig));
    }

    @Override
    public List<String> getLLMConfig(List<Variable> variableStructure) {
        return this.getLLMConfig();
    }

}
