package com.red.circle.console.app.service.app.approval;

import com.red.circle.console.app.convertor.approval.ApprovalDynamicReportAppConvertor;
import com.red.circle.console.app.dto.clienobject.approval.ApprovalDynamicReportedCO;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.dynamic.ApprovalDynamicReportClient;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDynamicReportCmd;
import com.red.circle.other.inner.model.cmd.approval.ApprovalDynamicReportQryCmd;
import com.red.circle.other.inner.model.dto.approval.ApprovalDynamicReportedDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 动态投诉
 *
 * @author zongpubin on 2024/1/19
 */
@Service
@RequiredArgsConstructor
public class ApprovalDynamicReportServiceImpl implements ApprovalDynamicReportService {

  private final UserProfileClient userProfileClient;
  private final ApprovalDynamicReportClient approvalDynamicReportClient;
  private final ApprovalDynamicReportAppConvertor approvalDynamicReportAppConvertor;

  @Override
  public PageResult<ApprovalDynamicReportedCO> pageData(ApprovalDynamicReportQryCmd query) {
    PageResult<ApprovalDynamicReportedDTO> pageResult = ResponseAssert.requiredSuccess(
        approvalDynamicReportClient.pageData(query));
    if (CollectionUtils.isEmpty(pageResult.getRecords())) {
      return pageResult.convert(approvalDynamicReportAppConvertor::toApprovalDynamicReportedCO);
    }
    Map<Long, UserProfileDTO> userMap = ResponseAssert.requiredSuccess(
        userProfileClient.mapByUserIds(getUserIds(pageResult)));

    var result = pageResult.convert(record -> {
      UserProfileDTO reportUser = userMap.get(record.getReportUserId());
      UserProfileDTO reportedUser = userMap.get(record.getReportedUserId());
      if (Objects.isNull(reportUser) || Objects.isNull(reportedUser)) {
        return null;
      }
      ApprovalDynamicReportedCO approvalDynamicReportedCO = approvalDynamicReportAppConvertor.toApprovalDynamicReportedCO(
          record);

      approvalDynamicReportedCO.setReportUser(reportUser);
      approvalDynamicReportedCO.setReportedUser(reportedUser);
      return approvalDynamicReportedCO;
    });
    result.setRecords(result.getRecords().stream().filter(Objects::nonNull).collect(
        Collectors.toList()));
    return result;
  }

  private Set<Long> getUserIds(PageResult<ApprovalDynamicReportedDTO> iPage) {
    Set<Long> userIds = iPage.getRecords().stream().map(ApprovalDynamicReportedDTO::getReportUserId)
        .collect(Collectors.toSet());
    userIds.addAll(
        iPage.getRecords().stream().map(ApprovalDynamicReportedDTO::getReportedUserId).collect(
            Collectors.toSet()));
    return userIds;
  }

  @Override
  public void report(ApprovalDynamicReportCmd cmd) {
    approvalDynamicReportClient.report(cmd);
  }
}
