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

import com.google.common.collect.Maps;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
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.MemberEntryTeamCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.model.user.ability.RegionConfig;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamMember;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.infra.database.mongo.entity.user.region.SysRegionConfig;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamMemberService;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.infra.database.mongo.service.user.region.SysRegionConfigService;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.asserts.team.TeamErrorCode;
import com.red.circle.other.inner.enums.team.TeamMemberRole;

import java.util.*;

import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 成员团队资料.
 *
 * @author pengliang on 2022/12/6
 */
@Component
@RequiredArgsConstructor
public class TeamEntryTeamQryExe {

  private final TeamAppConvertor teamAppConvertor;
  private final UserProfileGateway userProfileGateway;
  private final UserRegionGateway userRegionGateway;
  private final TeamMemberService teamMemberService;
  private final TeamProfileService teamProfileService;
  private final SysRegionConfigService sysRegionConfigService;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final EnumConfigCacheService enumConfigCacheService;

  public MemberEntryTeamCO execute(AppExtCommand cmd) {
    TeamMember teamMember = teamMemberService.getByMemberId(cmd.getReqUserId());
    ResponseAssert.notNull(TeamErrorCode.NOT_TEAM_MEMBER, teamMember);

    TeamProfile teamProfile = teamProfileService.getById(teamMember.getTeamId());
    ResponseAssert.notNull(TeamErrorCode.TEAM_NOT_FOUND, teamProfile);

    UserProfileDTO teamMemberProfile = userProfileAppConvertor.toUserProfileDTO(
        userProfileGateway.getByUserId(cmd.getReqUserId()));

    String reviewUserWhitelist = enumConfigCacheService.getValue(EnumConfigKey.REVIEW_USER_WHITELIST, cmd.getReqSysOrigin().getOrigin());
    List<String> whiteIds = StringUtils.isNotBlank(reviewUserWhitelist) ? Arrays.asList(reviewUserWhitelist.split(",")) : Collections.emptyList();
    String accountProcessWhitelist = enumConfigCacheService.getValue(EnumConfigKey.ACCOUNT_PROCESS_WHITELIST, cmd.getReqSysOrigin().getOrigin());
    List<String> accountProcess = StringUtils.isNotBlank(accountProcessWhitelist) ? Arrays.asList(accountProcessWhitelist.split(",")) : Collections.emptyList();

    RegionConfig regionConfig = userRegionGateway.getRegionConfigByUserId(cmd.getReqUserId());
    Map<String, String> diamondMetadata = regionConfig.getDiamondMetadata();
    Boolean showPlatformPolicy = !CollectionUtils.isEmpty(diamondMetadata) && !"false".equals(diamondMetadata.get("walletVisible"));

    return new MemberEntryTeamCO()
        .setMemberProfile(teamMemberProfile)
        .setViolation(whiteIds.contains(teamMemberProfile.getAccount()))
        .setProcess(accountProcess.contains(teamMemberProfile.getAccount()))
        .setTeamProfile(teamAppConvertor.toTeamProfile(teamProfile))
        .setTeamOwnProfile(Objects.equals(teamMember.getRole(), TeamMemberRole.OWN)
            ? teamMemberProfile
            : userProfileAppConvertor.toUserProfileDTO(
                userProfileGateway.getByUserId(teamProfile.getOwnUserId())))
        .setRole(teamMember.getRole().name())
        .setWalletWithdrawal(Objects.isNull(teamMember.getWalletWithdrawal()) || Objects.equals(
            teamMember.getWalletWithdrawal(), Boolean.TRUE))
        .setDiamondMetadata(Optional.ofNullable(
                        sysRegionConfigService.getById(teamProfile.getRegion())
                ).map(SysRegionConfig::getDiamondMetadata)
                .orElseGet(Maps::newHashMap))
        .setShowPlatformPolicy(!showPlatformPolicy)
        .setMetadata(Optional.ofNullable(
                    sysRegionConfigService.getById(teamProfile.getRegion())
                ).map(SysRegionConfig::getMetadata)
                .orElseGet(Maps::newHashMap)
        );
  }

  public UserProfileDTO getMemberProfile(AppExtCommand cmd) {
      return userProfileAppConvertor.toUserProfileDTO(
              userProfileGateway.getByUserId(cmd.getReqUserId()));
  }

}
