package com.emoon.mcp.server.service.impl;

import cn.hutool.core.map.MapUtil;
import com.emoon.mcp.server.service.IMcpService;
import com.emoon.mcp.server.service.httpclients.AdminClient;
import com.emoon.mcp.util.McpConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author destiny
 * @description:
 * @date 2025/10/14 09:27
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class McpServiceImpl implements IMcpService {

    private final AdminClient adminClient;


    /**
     * 根据工具名称分发处理请求到对应的具体处理方法
     *
     * @param tool   工具名称，用于标识要执行的具体操作
     * @param params 参数映射，包含执行工具所需的各种参数
     * @return 返回处理结果的键值对映射
     * @throws IllegalArgumentException 当工具名称无效时抛出异常
     */
    @Override
    public R<Object> dispatch(String tool, Map<String, Object> params) {
        // 根据不同的工具名称路由到对应的处理方法
        return switch (tool) {
            case McpConstants.MCP_TOOL_NAME_LOGIN_USER -> handleLoginUser(params);
            case McpConstants.MCP_TOOL_NAME_QUERY_KNOWLEDGE_LIST -> handleQueryKnowledgeList(params);
            case McpConstants.MCP_TOOL_NAME_HOSPITAL_ACTIVITY -> handleHospitalActivity(params);
            case McpConstants.MCP_TOOL_NAME_FIND_MEDICAL_RECORD -> handleFindMedicalRecord(params);
            default -> throw new IllegalArgumentException("Invalid tool name: " + tool);
        };
    }

    private R<Object> handleLoginUser(Map<String, Object> params) {
        log.info("handleLoginUser: {}", JsonUtils.toJsonString(params));
        Map<String, Object> loginResp = null;
        try {
            loginResp = adminClient.login(params);
        } catch (Exception e) {
            return R.fail("登录失败", e.getMessage());
        }
        log.info("handleLoginUser: {}", JsonUtils.toJsonString(loginResp));
        if (StringUtils.isEmpty(MapUtil.getStr(loginResp, "code"))) {
            // 如果 resp 不包含 code 字段，则说明登录失败
            return R.fail("登录失败");
        }
        return R.ok("登录成功", MapUtil.getStr(loginResp, "data"));
    }

    private R<Object> handleQueryKnowledgeList(Map<String, Object> params) {
        log.info("handleQueryKnowledgeList: {}", JsonUtils.toJsonString(params));
        String token = MapUtil.getStr(params, "token");
        if (StringUtils.isEmpty(token)) {
            return R.fail("缺少 token 参数");
        }
        Map<String, Object> resp = adminClient.knowledgeList(String.format("Bearer: %s", token), MapUtil.removeAny(params, "token"));
        if (MapUtil.isEmpty(resp)) {
            return R.fail("查询失败");
        }
        // TODO 需要读取 body
        return R.ok("查询成功", resp);
    }

    private R<Object> handleHospitalActivity(Map<String, Object> params) {
        log.info("handleHospitalActivity: {}", JsonUtils.toJsonString(params));
        return null;
    }

    private R<Object> handleFindMedicalRecord(Map<String, Object> params) {
        log.info("handleFindMedicalRecord: {}", JsonUtils.toJsonString(params));
        return null;
    }



















}
