package tech.easyflow.agent.runtime.document;

import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tech.easyflow.common.mq.core.MQMessage;
import tech.easyflow.common.mq.core.MQProducer;

import java.util.Date;
import java.util.UUID;

/**
 * Agent 文档读取任务生产者。
 */
@Service
public class AgentDocumentReadTaskProducer {

    private static final Logger LOG = LoggerFactory.getLogger(AgentDocumentReadTaskProducer.class);
    private final MQProducer mqProducer;

    /**
     * 创建任务生产者。
     *
     * @param mqProducer 通用 MQ 生产者
     */
    public AgentDocumentReadTaskProducer(MQProducer mqProducer) {
        this.mqProducer = mqProducer;
    }

    /**
     * 投递只携带附件 ID 的读取任务。
     *
     * @param attachmentId 附件 ID
     */
    public void send(String attachmentId) {
        AgentDocumentTaskMessage event = new AgentDocumentTaskMessage();
        event.setAttachmentId(attachmentId);
        event.setTraceId(UUID.randomUUID().toString());
        event.setOccurredAt(new Date());
        MQMessage message = new MQMessage();
        message.setMessageId("agent-document-read-" + attachmentId);
        message.setTopic(AgentDocumentTaskMqConstants.READ_TOPIC);
        message.setKey(attachmentId);
        message.setCreatedAt(event.getOccurredAt());
        message.setBody(JSON.toJSONString(event));
        String recordId = mqProducer.send(message);
        LOG.info("Agent 文档读取任务已投递: attachmentId={}, recordId={}", attachmentId, recordId);
    }
}
