package com.yanzhu.module.ai.util;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.yanzhu.framework.security.core.util.SecurityFrameworkUtils;
import com.yanzhu.framework.tenant.core.context.TenantContextHolder;
import com.yanzhu.module.ai.enums.model.AiPlatformEnum;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.model.output.Response;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import java.util.HashMap;

/**
 * Spring AI 工具类
 *
 * @author 研筑科技
 */
public class AiUtils {

    public static final String TOOL_CONTEXT_LOGIN_USER = "LOGIN_USER";
    public static final String TOOL_CONTEXT_TENANT_ID = "TENANT_ID";

    public static Map<String, Object> buildChatOptions(AiPlatformEnum platform, String model, Double temperature, Integer maxTokens) {
        return buildChatOptions(platform, model, temperature, maxTokens, null, null);
    }

    public static Map<String, Object> buildChatOptions(AiPlatformEnum platform, String model, Double temperature, Integer maxTokens,
                                               List<Object> toolCallbacks, Map<String, Object> toolContext) {
        toolCallbacks = ObjUtil.defaultIfNull(toolCallbacks, Collections.emptyList());
        toolContext = ObjUtil.defaultIfNull(toolContext, Collections.emptyMap());
        
        // 返回配置参数的Map，由具体的AI模型实现处理
        Map<String, Object> options = new HashMap<>();
        options.put("model", model);
        options.put("temperature", temperature);
        options.put("maxTokens", maxTokens);
        options.put("toolCallbacks", toolCallbacks);
        options.put("toolContext", toolContext);
        
        return options;
    }

    public static ChatMessage buildMessage(String type, String content) {
        if ("user".equals(type)) {
            return new UserMessage(content);
        }
        if ("assistant".equals(type)) {
            return new AiMessage(content);
        }
        if ("system".equals(type)) {
            return new SystemMessage(content);
        }
        if ("tool".equals(type)) {
            throw new UnsupportedOperationException("暂不支持 tool 消息：" + content);
        }
        throw new IllegalArgumentException(StrUtil.format("未知消息类型({})", type));
    }

    public static Map<String, Object> buildCommonToolContext() {
        Map<String, Object> context = new HashMap<>();
        context.put(TOOL_CONTEXT_LOGIN_USER, SecurityFrameworkUtils.getLoginUser());
        context.put(TOOL_CONTEXT_TENANT_ID, TenantContextHolder.getTenantId());
        return context;
    }

    public static String getChatResponseContent(Response<AiMessage> response) {
        if (response == null
                || response.content() == null
                || response.content().text() == null) {
            return null;
        }
        return response.content().text();
    }

    public static String getChatResponseReasoningContent(Response<AiMessage> response) {
        if (response == null
                || response.content() == null) {
            return null;
        }
        // LangChain4J通常不区分推理内容，直接返回内容即可
        return response.content().text();
    }

}