package com.red.circle.other.app.command.team.query;

import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.team.BdInviteMessageCO;
import com.red.circle.other.app.dto.cmd.team.BdInviteQryCmd;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.team.bd.BdInviteAgentMessage;
import com.red.circle.other.infra.database.mongo.service.team.bd.BdInviteAgentMessageService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * bd邀请消息查询.
 *
 * @author pengliang on 2022/10/10
 */
@Component
@RequiredArgsConstructor
public class BdInviteMessageQryExe {

  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final BdInviteAgentMessageService bdInviteAgentMessageService;

  public List<BdInviteMessageCO> execute(BdInviteQryCmd cmd) {
    List<BdInviteAgentMessage> messages = bdInviteAgentMessageService.listInviteMessage(
        cmd.requiredReqUserId(), cmd.getGroup(), 50);
    if (CollectionUtils.isEmpty(messages)) {
      return CollectionUtils.newArrayList();
    }

    Map<Long, UserProfileDTO> userProfileMap = userProfileAppConvertor.toMapUserProfileDTO(
        userProfileGateway.mapByUserIds(
            messages.stream().map(BdInviteAgentMessage::getUserId).collect(Collectors.toSet())
        )
    );
    return messages.stream().map(msg -> new BdInviteMessageCO()
        .setId(msg.getId())
        .setUserProfile(Optional.ofNullable(userProfileMap.get(msg.getUserId()))
            .map(userProfile -> {
              userProfile.setAccount(userProfile.actualAccount());
              return userProfile;
            })
            .orElse(null))
        .setMessage(msg.getMessage())
        .setStatus(msg.getStatus())
        .setGroup(msg.getGroup())
        .setCreateTime(msg.getCreateTime())
        .setExpiredTime(msg.getExpiredTime())
    ).collect(Collectors.toList());
  }

  public List<BdInviteMessageCO> inviteMessageOwn(BdInviteQryCmd cmd) {
    List<BdInviteAgentMessage> messages = bdInviteAgentMessageService.listInviteMessageOwn(cmd.requiredReqUserId(), cmd.getGroup(), 50);
    if (CollectionUtils.isEmpty(messages)) {
      return CollectionUtils.newArrayList();
    }

    Map<Long, UserProfileDTO> userProfileMap = userProfileAppConvertor.toMapUserProfileDTO(
        userProfileGateway.mapByUserIds(
            messages.stream().map(BdInviteAgentMessage::getInviteUserId).collect(Collectors.toSet())
        )
    );
    return messages.stream().map(msg -> new BdInviteMessageCO()
        .setId(msg.getId())
        .setUserProfile(Optional.ofNullable(userProfileMap.get(msg.getInviteUserId()))
            .map(userProfile -> {
              userProfile.setAccount(userProfile.actualAccount());
              return userProfile;
            })
            .orElse(null))
        .setMessage(msg.getMessage())
        .setStatus(msg.getStatus())
        .setGroup(msg.getGroup())
        .setCreateTime(msg.getCreateTime())
        .setExpiredTime(msg.getExpiredTime())
    ).collect(Collectors.toList());
  }


}
