package com.cmvr.llm.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import com.cmvr.llm.config.APIProperties;
import com.cmvr.llm.config.AgentConfig;
import com.cmvr.llm.service.LLMIntentRecognitionService;
import com.cmvr.llm.service.WorkflowInvokeService;
import com.cmvr.llm.util.LlmChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class LLMIntentRecognitionServiceImpl implements LLMIntentRecognitionService {

    private final APIProperties apiProperties;
    private final WorkflowInvokeService workflowInvokeService;
    private final LlmChatService llmChatService;
    @Override
    public JSONObject intentRecognition(String action, String text) {
        AgentConfig acg = apiProperties.getAgentConfig(action);
//        Map<String, String> inputParams = MapUtil.of("text", text);
//        return new JSONObject().fluentPut("type", "4").fluentPut("message", "\n\n建议优先选择成都大熊猫繁育研究基地（成华区），该基地拥有顶流熊猫\\\"花花\\\"及完善的参观体系。门票需提前7天在\\\"成都熊猫基地电子票\\\"公众号预约，全票55元/人，建议选择清晨7:30前抵达南门入园（当前9月凉爽适宜户外观赏）。重点参观点为幼年大熊猫别墅（花花所在地）、月亮/太阳产房（活跃时段8:30-10:30）。若需避开人流，可前往都江堰熊猫谷（门票51元），该区域植被覆盖率高，游客密度较低，适合深度观赏熊猫自然行为。交通建议乘坐地铁3号线至熊猫大道站，换乘景区直通车（运营时间8:00-17:00）。需注意园区禁止投喂，建议携带轻便饮食及防滑鞋具。");
//        return workflowInvokeService.executeWorkflow(acg.getAppId(), acg.getAppKey(), inputParams);
        // 调用
        Map<String, Object> body = new HashMap<>();
        body.put("Query", text);
        long startTime = System.currentTimeMillis();
        String result = null;
        try {
            result = llmChatService.chat(
                    acg.getAppKey(),  // apiKey (header用)
                    acg.getAppId(),  // apiId (body的AppKey)
                    "user_123",               // userId
                    body,
                    false,
                    null
            );
            System.out.println( result);
            System.out.println("总耗时：" + (System.currentTimeMillis() - startTime));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new JSONObject().fluentPut("type", result).fluentPut("test2", new JSONObject().fluentPut("test2-2", "今天天气怎么样"));
    }
}
