package tech.easyflow.agent.distributed;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tech.easyflow.agent.config.AgentRuntimeProperties;
import tech.easyflow.common.mq.core.MQMessage;
import tech.easyflow.common.mq.core.MQProducer;
import tech.easyflow.common.web.exceptions.BusinessException;

import java.math.BigInteger;
import java.util.Date;
import java.util.UUID;

/**
 * Agent 运行态远程命令生产者。
 */
@Service
public class AgentRuntimeCommandProducer {

    private static final Logger LOG = LoggerFactory.getLogger(AgentRuntimeCommandProducer.class);

    private final MQProducer mqProducer;
    private final ObjectMapper objectMapper;
    private final AgentRuntimeProperties properties;
    private final AgentRuntimeCommandResultRegistry resultRegistry;

    /**
     * 测试子类构造器。
     */
    protected AgentRuntimeCommandProducer() {
        this.mqProducer = null;
        this.objectMapper = null;
        this.properties = null;
        this.resultRegistry = null;
    }

    /**
     * 创建 Agent 运行态远程命令生产者。
     *
     * @param mqProducer MQ 生产者
     * @param objectMapper JSON 序列化器
     * @param properties Agent 运行配置
     * @param resultRegistry 远程命令结果注册表
     */
    public AgentRuntimeCommandProducer(MQProducer mqProducer,
                                       ObjectMapper objectMapper,
                                       AgentRuntimeProperties properties,
                                       AgentRuntimeCommandResultRegistry resultRegistry) {
        this.mqProducer = mqProducer;
        this.objectMapper = objectMapper;
        this.properties = properties;
        this.resultRegistry = resultRegistry;
    }

    /**
     * 投递远程批准命令。
     *
     * @param targetNodeId 目标节点 ID
     * @param requestId 请求 ID
     * @param resumeToken 恢复令牌
     * @param operatorId 操作人 ID
     * @param userId 用户 ID
     */
    public void sendApprove(String targetNodeId,
                            String requestId,
                            String resumeToken,
                            BigInteger operatorId,
                            String userId) {
        sendAndWait(targetNodeId, requestId, resumeToken, AgentRuntimeCommandAction.APPROVE, null, operatorId, userId);
    }

    /**
     * 投递远程拒绝命令。
     *
     * @param targetNodeId 目标节点 ID
     * @param requestId 请求 ID
     * @param resumeToken 恢复令牌
     * @param reason 拒绝原因
     * @param operatorId 操作人 ID
     * @param userId 用户 ID
     */
    public void sendReject(String targetNodeId,
                           String requestId,
                           String resumeToken,
                           String reason,
                           BigInteger operatorId,
                           String userId) {
        sendAndWait(targetNodeId, requestId, resumeToken, AgentRuntimeCommandAction.REJECT, reason, operatorId, userId);
    }

    private void sendAndWait(String targetNodeId,
                             String requestId,
                             String resumeToken,
                             AgentRuntimeCommandAction action,
                             String reason,
                             BigInteger operatorId,
                             String userId) {
        if (targetNodeId == null || targetNodeId.isBlank()) {
            throw new BusinessException("Agent 运行节点不可用，请重新发起对话");
        }
        AgentRuntimeCommandMessage command = new AgentRuntimeCommandMessage();
        command.setCommandId(UUID.randomUUID().toString());
        command.setRequestId(requestId);
        command.setResumeToken(resumeToken);
        command.setAction(action);
        command.setReason(reason);
        command.setOperatorId(operatorId);
        command.setUserId(userId);
        command.setTargetNodeId(targetNodeId);
        command.setOccurredAt(new Date());

        MQMessage message = new MQMessage();
        message.setMessageId(command.getCommandId());
        message.setTopic(commandTopic(targetNodeId));
        message.setKey(command.getCommandId());
        message.setCreatedAt(command.getOccurredAt());
        try {
            message.setBody(objectMapper.writeValueAsString(command));
            String recordId = mqProducer.send(message);
            LOG.info("Agent 远程运行命令已投递: action={}, requestId={}, targetNodeId={}, recordId={}",
                    action, requestId, targetNodeId, recordId);
            AgentRuntimeCommandResult result = resultRegistry.waitForResult(command.getCommandId());
            if (!result.isSuccess()) {
                throw new BusinessException(result.getMessage());
            }
        } catch (JsonProcessingException e) {
            throw new BusinessException("Agent 运行命令序列化失败");
        } catch (BusinessException e) {
            throw e;
        } catch (RuntimeException e) {
            LOG.error("Agent 远程运行命令投递失败: action={}, requestId={}, targetNodeId={}",
                    action, requestId, targetNodeId, e);
            throw new BusinessException("Agent 运行节点不可用，请重新发起对话");
        } finally {
            deleteResultQuietly(command.getCommandId());
        }
    }

    private String commandTopic(String nodeId) {
        return properties.getCommandTopicPrefix() + ":" + nodeId;
    }

    private void deleteResultQuietly(String commandId) {
        try {
            resultRegistry.deleteResult(commandId);
        } catch (RuntimeException e) {
            LOG.warn("Agent 远程运行命令结果清理失败，等待 TTL 兜底: commandId={}", commandId, e);
        }
    }
}
