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

import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.other.app.convertor.team.TeamAppConvertor;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.team.TeamBillMemberWorkCO;
import com.red.circle.other.app.dto.clientobject.team.TeamMemberWorkTargetCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamBillCycle;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMemberTarget;
import com.red.circle.other.infra.database.mongo.service.team.TeamBillCycleUtils;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamBillCycleService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberTargetService;
import com.red.circle.other.infra.database.rds.entity.team.BillDiamondBalance;
import com.red.circle.other.infra.database.rds.service.team.BillDiamondBalanceService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.enums.team.TeamMemberTargetSettlementResult;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.num.NumUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 团队账单工作成员.
 *
 * @author pengliang on 2022/12/27
 */
@Component
@RequiredArgsConstructor
public class TeamBillWorkMembersQryExe {

  private static final Logger log = LoggerFactory.getLogger(TeamBillWorkMembersQryExe.class);
  private final TeamAppConvertor teamAppConvertor;
  private final UserProfileGateway userProfileGateway;
  private final TeamBillCycleService teamBillCycleService;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final TeamMemberTargetService teamMemberTargetService;
  private final BillDiamondBalanceService billDiamondBalanceService;

  public List<TeamBillMemberWorkCO> execute(AppIdLongCmd cmd) {

    TeamBillCycle teamBillCycle = teamBillCycleService.getByBillId(cmd.getId());

    if (Objects.isNull(teamBillCycle)) {
      return Lists.newArrayList();
    }

    List<TeamMemberTarget> teamMemberTargets = teamMemberTargetService.listByBillBelong(
        teamBillCycle.getTeamId(), teamBillCycle.getBillBelong());

    if (CollectionUtils.isEmpty(teamMemberTargets)) {
      return Lists.newArrayList();
    }

    Map<Long, UserProfileDTO> userProfileMap = userProfileAppConvertor.toMapUserProfileDTO(
        userProfileGateway.mapByUserIds(
            teamMemberTargets.stream().map(TeamMemberTarget::getUserId)
                .collect(Collectors.toSet())));

    return teamMemberTargets.stream().map(target -> new TeamBillMemberWorkCO()
        .setMemberProfile(userProfileMap.get(target.getUserId()))
        .setTarget(assembleTarget(target))
    ).toList();
  }

  private TeamMemberWorkTargetCO assembleTarget(TeamMemberTarget target) {
    TeamMemberTargetSettlementResult settlementResult = target.getSettlementResult();
    if (Objects.isNull(settlementResult)) {
      return new TeamMemberWorkTargetCO()
          .setId(target.getTeamId())
          .setAcceptGiftValue(target.sumAcceptGiftValue())
          .setAcceptGiftValueFormat(NumUtils.formatLong(target.sumAcceptGiftValue()))
           .setEffectiveDay(TeamBillCycleUtils.calculateEffectiveDay(target))
          .setOnlineTime(target.sumAllTargetTime());
    }
    return new TeamMemberWorkTargetCO()
        .setId(target.getTeamId())
        .setAcceptGiftValue(settlementResult.getAcceptGiftValue())
        .setAcceptGiftValueFormat(NumUtils.formatLong(settlementResult.getAcceptGiftValue()))
        .setOnlineTime(settlementResult.getOnlineTime())
         .setEffectiveDay(TeamBillCycleUtils.calculateEffectiveDay(target))
        .setSettlementResult(
            teamAppConvertor.toTeamMemberTargetSettlementResultCO(settlementResult));
  }
}
