package com.red.circle.console.app.service.app.team;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.red.circle.console.app.convertor.team.TeamAppConvertor;
import com.red.circle.console.app.dto.clienobject.team.TeamPolicyManagerCO;
import com.red.circle.console.infra.database.rds.service.admin.UserService;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.inner.endpoint.team.target.TeamPolicyManagerClient;
import com.red.circle.other.inner.model.cmd.team.TeamPolicyManagerBackQryCmd;
import com.red.circle.other.inner.model.cmd.team.TeamPolicyManagerCmd;
import com.red.circle.other.inner.model.dto.agency.agency.TeamPolicyManagerDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 团队策略.
 *
 * @author pengshigang on 2022/12/12
 */
@Service
@RequiredArgsConstructor
public class TeamPolicyManagerBackServiceImpl implements TeamPolicyManagerBackService {

  private final UserService userService;
  private final TeamAppConvertor teamAppConvertor;
  private final TeamPolicyManagerClient teamPolicyManagerClient;

  @Override
  public List<TeamPolicyManagerCO> historyReleases(TeamPolicyManagerBackQryCmd query) {
    return assembleTeamPolicyManagerVO(ResponseAssert.requiredSuccess(teamPolicyManagerClient
        .listSysOriginRegion(query.getSysOrigin(), query.getRegion(), query.getPolicyType(), query.getCountryCode())));
  }

  @Override
  public TeamPolicyManagerCO getByRegionByType(TeamPolicyManagerBackQryCmd query) {
    TeamPolicyManagerDTO teamPolicyManager = ResponseAssert.requiredSuccess(teamPolicyManagerClient
        .getReleaseByRegionAndType(query.getSysOrigin(), query.getRegion(), query.getPolicyType(), query.getCountryCode()));
    if (Objects.isNull(teamPolicyManager)) {
      return new TeamPolicyManagerCO();
    }
    TeamPolicyManagerCO managerVO = teamAppConvertor.toTeamPolicyManagerCO(teamPolicyManager);
    if (Objects.nonNull(managerVO.getCreateUser())) {
      managerVO.setCreateUserNickname(userService.getById(managerVO.getCreateUser()).getNickname());
    }
    return managerVO;
  }

  @Override
  public List<TeamPolicyManagerCO> listTeamPolicy(String sysOrigin, String region) {
    return assembleTeamPolicyManagerVO(
        ResponseAssert.requiredSuccess(teamPolicyManagerClient.listTeamPolicy(sysOrigin, region)));
  }

  @Override
  public void add(TeamPolicyManagerCmd policyManager) {
    teamPolicyManagerClient.add(policyManager);

  }

  @Override
  public void deleteById(Long id) {
    teamPolicyManagerClient.deleteById(id);
  }

  private List<TeamPolicyManagerCO> assembleTeamPolicyManagerVO(
      List<TeamPolicyManagerDTO> historyReleases) {
    if (CollectionUtils.isEmpty(historyReleases)) {
      return Lists.newArrayList();
    }

    Map<Long, String> mapBackUserNickname = userService.mapBackUserNickname(
        historyReleases.stream().map(TeamPolicyManagerDTO::getCreateUser)
            .collect(Collectors.toSet()));
    if (CollectionUtils.isEmpty(mapBackUserNickname)) {
      return Lists.newArrayList();
    }

    return historyReleases.stream().map(history -> {
      TeamPolicyManagerCO managerVO = teamAppConvertor.toTeamPolicyManagerCO(history);
      managerVO.setCreateUserNickname(mapBackUserNickname.get(history.getCreateUser()));
      return managerVO;
    }).collect(Collectors.toList());
  }
}
