package com.sino.mci.inbound.mq;

import com.sino.mci.inbound.engine.FlowEngine;
import com.sino.mci.inbound.entity.MessageEvent;
import com.sino.mci.inbound.repository.MessageEventMapper;
import com.sino.mci.mq.config.MqConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 入站流程事件消费者。
 *
 * <p>消费 {@code mci.inbound.flow.queue} 中意图识别后的事件，
 * 加载完整 {@link MessageEvent} 并交由 {@link FlowEngine} 执行入站流程。</p>
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class InboundFlowConsumer {

    private final FlowEngine flowEngine;
    private final MessageEventMapper messageEventMapper;

    @RabbitListener(queues = MqConfig.INBOUND_FLOW_QUEUE)
    public void handle(InboundEventMessage message) {
        log.info("收到入站流程事件: eventId={}", message.getEventId());

        MessageEvent event = messageEventMapper.selectById(message.getId());
        if (event == null) {
            log.warn("入站流程事件不存在: eventId={}", message.getEventId());
            return;
        }

        flowEngine.executeInboundFlow(event.getId());
        log.info("入站流程执行完成: eventId={}", event.getEventId());
    }
}
