package com.sino.mci.inbound.mq;

import com.sino.mci.inbound.entity.MessageEvent;
import com.sino.mci.mq.config.MqConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 意图识别后的事件 MQ 生产者。
 *
 * <p>将已识别意图的入站消息事件发送到 {@code mci.inbound.flow.exchange}，
 * 供下游流程引擎（Phase 3.8）消费。</p>
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class InboundFlowMessageProducer {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 发布意图识别后的入站消息事件。
     *
     * @param event 消息事件
     */
    public void publish(MessageEvent event) {
        InboundEventMessage message = convert(event);
        rabbitTemplate.convertAndSend(MqConfig.INBOUND_FLOW_EXCHANGE,
                MqConfig.INBOUND_FLOW_ROUTING_KEY, message);
        log.info("Inbound flow event published: eventId={}", event.getEventId());
    }

    private InboundEventMessage convert(MessageEvent event) {
        InboundEventMessage message = new InboundEventMessage();
        message.setId(event.getId());
        message.setEventId(event.getEventId());
        message.setTenantCode(event.getTenantCode());
        message.setChannelType(event.getChannelType());
        message.setChannelSubjectId(event.getChannelSubjectId());
        message.setDirection(event.getDirection());
        message.setEventType(event.getEventType());
        message.setMessageSource(event.getMessageSource());
        message.setConversationId(event.getConversationId());
        message.setPersonId(event.getPersonId());
        message.setAccountId(event.getAccountId());
        message.setContent(event.getContent());
        message.setRawPayload(event.getRawPayload());
        message.setBusinessContext(event.getBusinessContext());
        message.setIsProcessed(event.getIsProcessed());
        message.setStatus(event.getStatus());
        message.setCreateTime(event.getCreateTime() != null ? event.getCreateTime().toString() : null);
        return message;
    }
}
