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

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.app.AppTypeCmd;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.activity.AgentActivityHistoryCO;
import com.red.circle.other.app.dto.clientobject.activity.AgentActivityOwnCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.infra.database.mongo.entity.activity.AgentActivityCount;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.infra.database.mongo.service.activity.AgentActivityCountService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.num.NumUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 代理活动历史用户数据 - 周/月.
 * </p>
 *
 * @author pengshigang
 * @since 2023-05-29
 */
@Component
@RequiredArgsConstructor
public class AgentActivityHistoryUserQueryExe {

  private final UserRegionGateway userRegionGateway;
  private final TeamProfileService teamProfileService;
  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final AgentActivityCountService agentActivityCountService;

  public List<AgentActivityHistoryCO> execute(AppTypeCmd cmd) {

    String regionId = userRegionGateway.getRegionId(cmd.getReqUserId());
    if (StringUtils.isBlank(regionId)) {
      return List.of();
    }

    List<AgentActivityCount> counts = agentActivityCountService
        .historyLimit33(regionId, cmd.getType());
    if (CollectionUtils.isEmpty(counts)) {
      return List.of();
    }
    Map<Long, TeamProfile> mapTeamProfile = mapTeamProfiles(counts);
    if (CollectionUtils.isEmpty(mapTeamProfile)) {
      return List.of();
    }

    Map<Integer, List<AgentActivityCount>> listMap = counts.stream()
        .collect(Collectors.groupingBy(AgentActivityCount::getDateNumber));

    List<AgentActivityHistoryCO> historyList = Lists.newArrayList();
    listMap.forEach((key, list) -> historyList.add(new AgentActivityHistoryCO()
            .setDesc(list.get(0).getDateDesc())
            .setUsers(list.stream().map(count -> {

                  TeamProfile teamProfile = mapTeamProfile.get(count.getTeamId());
                  if (Objects.isNull(teamProfile)) {
                    return null;
                  }

                  UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO(
                      userProfileGateway.getByUserId(teamProfile.getOwnUserId())
                  );
                  if (Objects.isNull(userProfile)) {
                    return null;
                  }

                  return new AgentActivityOwnCO()
                      .setUserProfile(userProfile)
                      .setQuantity(count.getTarget() > 0 ? NumUtils.formatLong(count.getTarget()) : "0");

                }).filter(Objects::nonNull).toList()
            )
        )
    );
    return historyList;

  }

  private Map<Long, TeamProfile> mapTeamProfiles(List<AgentActivityCount> counts) {
    return teamProfileService.mapProfileByIds(
        counts.stream().map(AgentActivityCount::getTeamId).collect(Collectors.toSet()));
  }

}
