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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.activity.WeekStarHistoryVO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.activity.WeekStarGiftCount;
import com.red.circle.other.infra.database.mongo.service.activity.WeekStarGiftCountService;
import com.red.circle.other.inner.enums.activity.KingQueenType;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 周星历史TOP1查询执行器.
 *
 * @author system
 */
@Component
@RequiredArgsConstructor
public class WeekStarHistoryTop1QueryExe {

  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final WeekStarGiftCountService weekStarGiftCountService;

  public List<WeekStarHistoryVO> execute(AppExtCommand cmd) {
    // 获取历史TOP1数据
    List<WeekStarGiftCount> historyTop1List = weekStarGiftCountService
        .getHistoryTop1(cmd.getReqSysOrigin().getOrigin(), 20);

    if (CollectionUtils.isEmpty(historyTop1List)) {
      return List.of();
    }

    // 获取用户信息
    Map<Long, UserProfileDTO> userProfileMap = getUserProfileMap(historyTop1List);

    // 转换为返回对象
    return historyTop1List.stream()
        .map(item -> convertToWeekStarHistoryVO(item, userProfileMap))
        .filter(Objects::nonNull)
        .collect(Collectors.toList());
  }

  private Map<Long, UserProfileDTO> getUserProfileMap(List<WeekStarGiftCount> historyTop1List) {
    Set<Long> userIds = historyTop1List.stream()
        .map(WeekStarGiftCount::getUserId)
        .collect(Collectors.toSet());

    return userProfileAppConvertor.toMapUserProfileDTO(
        userProfileGateway.mapByUserIds(userIds)
    );
  }

  private WeekStarHistoryVO convertToWeekStarHistoryVO(WeekStarGiftCount weekStarGiftCount, 
                                                      Map<Long, UserProfileDTO> userProfileMap) {
    if (Objects.isNull(weekStarGiftCount)) {
      return null;
    }

    UserProfileDTO userProfile = userProfileMap.get(weekStarGiftCount.getUserId());
    if (Objects.isNull(userProfile)) {
      return null;
    }

    WeekStarHistoryVO historyVO = new WeekStarHistoryVO();
    historyVO.setId(weekStarGiftCount.getUserId() + "_" + weekStarGiftCount.getGroup());
    historyVO.setSysOrigin(weekStarGiftCount.getSysOrigin());
    historyVO.setAccount(userProfile.getAccount());
    historyVO.setUserAvatar(userProfile.getUserAvatar());
    historyVO.setUserNickname(userProfile.getUserNickname());
    historyVO.setUserId(weekStarGiftCount.getUserId());
    historyVO.setUserSex(userProfile.getUserSex());
    historyVO.setRank(weekStarGiftCount.getRank());
    historyVO.setGroup(weekStarGiftCount.getGroup().replaceAll("(\\d{4})(\\d{2})(\\d{2})", "$1-$2-$3"));
    historyVO.setQuantity(weekStarGiftCount.getQuantity());
    
    // 转换时间类型
    if (Objects.nonNull(weekStarGiftCount.getCreateTime())) {
      historyVO.setCreateTime(weekStarGiftCount.getCreateTime());
    }
    if (Objects.nonNull(weekStarGiftCount.getExpiredTime())) {
      historyVO.setExpiredTime(weekStarGiftCount.getExpiredTime());
    }

    return historyVO;
  }

}
