package com.red.circle.other.app.inner.service.team.impl;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.infra.convertor.team.TeamInfraConvertor;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamPolicyManager;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamPolicyManagerService;
import com.red.circle.other.app.inner.service.team.TeamPolicyManagerClientService;
import com.red.circle.other.inner.enums.team.TeamPolicyStatusEnum;
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.sequence.IdWorkerUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 团队策略.
 *
 * @author pengliang on 2023/8/19
 */
@Service
@RequiredArgsConstructor
public class TeamPolicyManagerClientServiceImpl implements TeamPolicyManagerClientService {

  private final TeamInfraConvertor teamAppConvertor;
  private final TeamPolicyManagerService teamPolicyManagerService;


  @Override
  public TeamPolicyManagerDTO getReleaseByRegionAndType(String sysOrigin, String region) {
    return teamAppConvertor.toTeamPolicyManagerDTO(
        teamPolicyManagerService.getReleaseByRegionAndType(sysOrigin, region));
  }

  @Override
  public TeamPolicyManagerDTO getReleaseByRegionAndTypePolicyType(String sysOrigin, String region,
                                                                  String policyType, String countryCode) {
    return teamAppConvertor.toTeamPolicyManagerDTO(
            teamPolicyManagerService.getReleaseByRegionAndType(sysOrigin, region, policyType, countryCode));
  }


  @Override
  public Map<String, TeamPolicyManagerDTO> mapRegionRelease(Set<String> regions) {
    return teamAppConvertor.toMapTeamPolicyManagerDTO(
        teamPolicyManagerService.mapRegionRelease(regions));
  }

  @Override
  public List<TeamPolicyManagerDTO> listSysOriginRegion(String sysOrigin, String region) {
    return teamAppConvertor.toListTeamPolicyManagerDTO(
        teamPolicyManagerService.listSysOriginRegion(sysOrigin, region));
  }

  @Override
  public List<TeamPolicyManagerDTO> listTeamPolicy(String sysOrigin, String region) {
    return teamAppConvertor.toListTeamPolicyManagerDTO(
        teamPolicyManagerService.listTeamPolicy(sysOrigin, region));
  }

  @Override
  public List<TeamPolicyManagerDTO> listTeamPolicyAndTypePolicy(String sysOrigin, String region,
                                                                String policyType, String countryCode) {
    return teamAppConvertor.toListTeamPolicyManagerDTO(
            teamPolicyManagerService.listTeamPolicyAndTypePolicy(sysOrigin, region, policyType, countryCode));
  }

  @Override
  public void add(TeamPolicyManagerCmd cmd) {
    if (Objects.nonNull(cmd.getId())) {
      TeamPolicyManager existingPolicy = teamPolicyManagerService.getById(cmd.getId());
      if (Objects.nonNull(existingPolicy) && shouldReplaceExistingPolicy(cmd, existingPolicy)) {
        teamPolicyManagerService.deleteById(cmd.getId());
      }
    }
    cmd.setId(IdWorkerUtils.getId());
    teamPolicyManagerService.add(teamAppConvertor.toTeamPolicyManager(cmd));
  }

  private boolean shouldReplaceExistingPolicy(TeamPolicyManagerCmd cmd, TeamPolicyManager existingPolicy) {
    if (!Objects.equals(cmd.getPolicyStatus(), TeamPolicyStatusEnum.SCHEDULED.name())) {
      return true;
    }
    return Objects.equals(existingPolicy.getPolicyStatus(), TeamPolicyStatusEnum.SCHEDULED.name())
        && !Boolean.TRUE.equals(existingPolicy.getHistoryRelease())
        && !Boolean.TRUE.equals(existingPolicy.getRelease());
  }


  @Override
  public void deleteById(Long id) {
    TeamPolicyManager teamPolicyManager = teamPolicyManagerService.getById(id);
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, teamPolicyManager);
    teamPolicyManagerService.deleteById(id);
  }

}
