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

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.activity.TemporaryActivityCountCO;
import com.red.circle.other.app.dto.cmd.activity.TemporaryActivityQueryCmd;
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.TemporaryActivityCount;
import com.red.circle.other.infra.database.mongo.service.activity.TemporaryActivityCountService;
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 java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 活动排行榜.
 *
 * @author pengliang on 2022/8/24
 */
@Component
@RequiredArgsConstructor
public class TemporaryActivityQueryExe {

  private final UserRegionGateway userRegionGateway;
  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final TemporaryActivityCountService temporaryActivityCountService;

  public List<TemporaryActivityCountCO> execute(TemporaryActivityQueryCmd cmd) {

//    String countType = "RANK";

    List<TemporaryActivityCount> temporaryActivityCounts = temporaryActivityCountService.list(
        cmd.getReqSysOrigin().getOrigin(),
        cmd.getActivityType(),
        cmd.getCountType(),
        Objects.isNull(cmd.getLimit()) ? 50 : cmd.getLimit() > 100 ? 100 : cmd.getLimit());

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

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

    return temporaryActivityCounts.stream()
        .map(count -> new TemporaryActivityCountCO()
            .setQuantity(count.getQuantity())
            .setQuantityFormat(NumUtils.formatLong(count.getQuantity()))
            .setUserProfile(userProfileMap.get(count.getUserId()))
        )
        .toList();
  }

  public List<TemporaryActivityCountCO> executeSend(TemporaryActivityQueryCmd cmd) {

    List<TemporaryActivityCount> temporaryActivityCounts = temporaryActivityCountService.list(
            cmd.getReqSysOrigin().getOrigin(),
            cmd.getActivityType(),
            cmd.getCountType(),
            10);

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

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

    return temporaryActivityCounts.stream()
            .map(count -> new TemporaryActivityCountCO()
                    .setQuantity(count.getQuantity())
                    .setUserProfile(userProfileMap.get(count.getUserId()))
            )
            .toList();
  }

}
