package com.sikey.wa04.business.application.service.message;

import cn.hutool.json.JSONUtil;
import com.sikey.wa04.business.application.converter.MessageConverter;
import com.sikey.wa04.business.domain.adapter.MessageAdapter;
import com.sikey.wa04.business.domain.entity.message.Message;
import com.sikey.wa04.business.domain.entity.message.MessageId;
import com.sikey.wa04.business.infrastructure.adapter.RabbitMQMessaging;
import com.sikey.wa04.business.infrastructure.adapter.RedisMessageIdIncrementingAdapter;
import com.sikey.wa04.business.infrastructure.dto.request.messaging.ContactMessageRequest;
import com.sikey.wa04.common.dto.header.GenericManufacturerHeader;
import com.sikey.wa04.common.dto.message.ContactChangeNoticeMessage;
import com.sikey.wa04.common.enums.MessageType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;

/**
 * @Author: nelson
 * @Date: 2025/6/13
 * @Description: 通讯录消息
 */
@Slf4j
@Service
public class ContactMessageService {

    @Resource
    private RedisMessageIdIncrementingAdapter messageIdIncrementingAdapter;

    @Resource
    private RabbitMQMessaging rabbitMQMessaging;

    @Resource
    private MessageAdapter messageAdapter;

    /**
     * 通讯录变更消息
     *
     * @param contactMessageRequest 通讯录变更消息
     * @param currentUserId         当前登录用户编码
     * @return
     */
    public void contactMessageChange(ContactMessageRequest contactMessageRequest, String currentUserId) {
        log.info("[通讯录变更消息]入参: {}", JSONUtil.toJsonPrettyStr(contactMessageRequest));
        Instant sendTime = Instant.now();
        String cid = contactMessageRequest.getCid();
        String userId = contactMessageRequest.getUserId();

        // 手表
        Message message = new Message()
                .setId(new MessageId(messageIdIncrementingAdapter.incr()))
                .setType(MessageType.CONTACT_CHANGE_NOTICE_MESSAGE)
                .setContent(new ContactChangeNoticeMessage.ContactChangeNoticeContent().setCid(cid).setRole(contactMessageRequest.getRole()).setUserId(userId))
                .setGroup(false)
                .setSenderId(currentUserId)
                .setReceiverId(List.of(cid))
                .setSendTime(sendTime);
        messageAdapter.save(message);
        publish(contactMessageRequest.getHeader(), message);

        // 用户
        message = new Message()
                .setId(new MessageId(messageIdIncrementingAdapter.incr()))
                .setType(MessageType.CONTACT_CHANGE_NOTICE_MESSAGE)
                .setContent(new ContactChangeNoticeMessage.ContactChangeNoticeContent().setCid(cid).setRole(contactMessageRequest.getRole()).setUserId(userId))
                .setGroup(false)
                .setSenderId(currentUserId)
                .setReceiverId(List.of(userId))
                .setSendTime(sendTime);
        messageAdapter.save(message);
        publish(contactMessageRequest.getHeader(), message);
    }

    /**
     * 通讯录变更消息只推送手表端
     *
     * @param contactMessageRequest 通讯录变更消息
     * @param currentUserId         当前登录用户编码
     * @return
     */
    public void contactMessageChangeWatch(ContactMessageRequest contactMessageRequest, String currentUserId) {
        log.info("[通讯录变更消息]通知手表入参: {}", JSONUtil.toJsonPrettyStr(contactMessageRequest));
        Instant sendTime = Instant.now();
        String cid = contactMessageRequest.getCid();
        String userId = contactMessageRequest.getUserId();

        // 手表
        Message message = new Message()
                .setId(new MessageId(messageIdIncrementingAdapter.incr()))
                .setType(MessageType.CONTACT_CHANGE_NOTICE_MESSAGE)
                .setContent(new ContactChangeNoticeMessage.ContactChangeNoticeContent().setCid(cid).setRole(contactMessageRequest.getRole()).setUserId(userId))
                .setGroup(false)
                .setSenderId(currentUserId)
                .setReceiverId(List.of(cid))
                .setSendTime(sendTime);
        messageAdapter.save(message);
        publish(contactMessageRequest.getHeader(), message);
    }


    private void publish(GenericManufacturerHeader headers, Message message) {
        log.info("Send message {}", message);
        Object messagingObject = MessageConverter.toMessaging(message);
        rabbitMQMessaging.publish(headers, messagingObject);
    }

}
