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

import com.red.circle.console.infra.database.rds.entity.admin.User;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.inner.endpoint.user.user.AppUserAccountClient;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.other.inner.model.cmd.user.UserAccountStatusLogQryCmd;
import com.red.circle.other.inner.model.dto.user.ApprovalUserAccountStatusLogDTO;
import java.util.List;
import java.util.Map;
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/2/26
 */
@RequiredArgsConstructor
@Service
public class UserAccountStatusLogServiceImpl implements
    UserAccountStatusLogService {

  private final UserService userService;
  private final UserProfileClient userProfileClient;
  private final AppUserAccountClient appUserAccountClient;


  @Override
  public PageResult<ApprovalUserAccountStatusLogDTO> pgeApprovalUserAccountStatusLog(
      UserAccountStatusLogQryCmd cmd) {
    PageResult<ApprovalUserAccountStatusLogDTO> pageResult = ResponseAssert.requiredSuccess(
        appUserAccountClient.pgeApprovalUserAccountStatusLog(cmd));
    if(CollectionUtils.isEmpty(pageResult.getRecords())){
      return pageResult;
    }

    return pageResult.convert(accountStatusLogDTO -> {
      accountStatusLogDTO.setApprovalUserName(getApprovalUserName(accountStatusLogDTO));
      return accountStatusLogDTO;
    });
  }

  @Override
  public List<ApprovalUserAccountStatusLogDTO> lastApprovalUserAccountStatusLog(Long beApprovalUser,
      Integer size) {
    List<ApprovalUserAccountStatusLogDTO> accountStatusLogs = ResponseAssert.requiredSuccess(
        appUserAccountClient.lastApprovalUserAccountStatusLog(beApprovalUser, size));

    if (CollectionUtils.isEmpty(accountStatusLogs)) {
      return accountStatusLogs;
    }
    Map<Long, User> userBaseInfoMap = userService.mapBackUser(
        accountStatusLogs.stream().map(ApprovalUserAccountStatusLogDTO::getCreateUser)
            .distinct().collect(Collectors.toList()));

    return accountStatusLogs.stream().peek(approvalUserAccountStatusLog -> {
      User user = userBaseInfoMap.get(approvalUserAccountStatusLog.getCreateUser());
      approvalUserAccountStatusLog
          .setApprovalUserName(Objects.nonNull(user) ? user.getNickname() : "机器");
    }).collect(Collectors.toList());
  }

  private String getApprovalUserName(ApprovalUserAccountStatusLogDTO statusLog) {
    return statusLog.getOriginType() ? Optional
        .ofNullable(ResponseAssert.requiredSuccess(userProfileClient.getByUserId(statusLog.getCreateUser()))).
        map(UserProfileDTO::getUserNickname).orElse("机器")
        : Optional.ofNullable(userService.getById(statusLog.getCreateUser())).
            map(User::getNickname).orElse("机器");
  }
}
