package com.red.circle.other.app.inner.service.user.user.impl;

import com.red.circle.common.business.core.enums.ApprovalStatusEnum;
import com.red.circle.common.business.core.enums.DataApprovalTypeEnum;
import com.red.circle.common.business.enums.ViolationEnum;
import com.red.circle.component.mq.MessageEvent;
import com.red.circle.component.mq.service.SenderService;
import com.red.circle.mq.business.model.event.approval.ApprovalContentEvent;
import com.red.circle.mq.business.model.event.approval.ApprovalDynamicEvent;
import com.red.circle.mq.rocket.business.streams.ConsoleApprovalSink;
import com.red.circle.other.app.inner.service.user.user.ApprovalPhotoWallClientService;
import com.red.circle.other.infra.database.rds.service.approval.ApprovalUserSettingDataService;
import com.red.circle.other.infra.database.rds.service.user.user.UserPhotoWallService;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDataCmd;
import com.red.circle.other.inner.model.cmd.user.ApprovalContentCmd;
import com.red.circle.other.inner.model.cmd.user.ApprovalPhotoWallCmd;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 照片墙审批
 *
 * @author zongpubin on 2024/1/20
 */
@Service
@RequiredArgsConstructor
public class ApprovalPhotoWallClientServiceImpl implements ApprovalPhotoWallClientService {

  private final SenderService senderService;
  private final UserPhotoWallService userPhotoWallService;
  private final ApprovalUserSettingDataService approvalUserSettingDataService;


  @Override
  public void approval(ApprovalPhotoWallCmd cmd) {

    // 修审批状态
    if (Objects.equals(cmd.getApprovalType(), DataApprovalTypeEnum.PHOTO_WALL.name())) {
      userPhotoWallService.updateViolationStatus(getWaitApprovalUserContentIds(cmd.getParams()),
          Objects.equals(cmd.getApprovalStatus(), ApprovalStatusEnum.PASS.name())
              ? ViolationEnum.NORMAL : ViolationEnum.VIOLATION
          , cmd.getApprovalUserId());
    }

    approvalUserSettingDataService.updateStatus(new ApprovalDataCmd()
        .setUserIds(getWaitApprovalUserIds(cmd.getParams()))
        .setApprovalUserId(cmd.getApprovalUserId())
        .setApprovalStatus(cmd.getApprovalStatus())
        .setApprovalType(cmd.getApprovalType())
    );

    senderService.sendEventMessage(MessageEvent.builder()
        .consumeId(IdWorkerUtils.getIdStr())
        .tag(ConsoleApprovalSink.TAG)
        .body(new ApprovalDynamicEvent()
            .setApprovalType(DataApprovalTypeEnum.PHOTO_WALL)
            .setOriginType(Boolean.FALSE)
            .setApprovalId(IdWorkerUtils.getId())
            .setApprovalUserId(cmd.getApprovalUserId())
            .setApprovalStatus(ApprovalStatusEnum.valueOf(cmd.getApprovalStatus()))
            .setWaitApprovalUser(
                cmd.getParams().parallelStream().map(approvalContent -> new ApprovalContentEvent()
                        .setContent(approvalContent.getContent())
                        .setContentId(approvalContent.getContentId())
                        .setUserId(approvalContent.getUserId())
                        .setTags(approvalContent.getTags())).filter(Objects::nonNull)
                    .collect(Collectors.toList()))).build());

  }

  private List<Long> getWaitApprovalUserIds(List<ApprovalContentCmd> params) {
    return Optional.ofNullable(params)
        .map(beApprovalList -> beApprovalList.stream().map(ApprovalContentCmd::getUserId)
            .distinct().collect(Collectors.toList()))
        .orElse(null);
  }

  private List<Long> getWaitApprovalUserContentIds(List<ApprovalContentCmd> params) {
    return Optional.ofNullable(params)
        .map(beApprovalList -> beApprovalList.stream().map(ApprovalContentCmd::getContentId)
            .distinct().collect(Collectors.toList()))
        .orElse(null);
  }
}
