package com.red.circle.wallet.app.command.bank.query;

import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.inner.endpoint.team.target.TeamBillCycleClient;
import com.red.circle.other.inner.endpoint.user.region.UserRegionClient;
import com.red.circle.other.inner.model.dto.agency.agency.TeamBillCycleDTO;
import com.red.circle.other.inner.model.dto.user.reigon.RegionConfigDTO;
import com.red.circle.wallet.app.common.BankRoleCheckCommon;
import com.red.circle.wallet.app.dto.clientobject.UserSalaryDiamondCheckExchangeGoldCO;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 进行中兑换金币检查.
 *
 * @author pengliang on 2023/2/24
 */
@Component
@RequiredArgsConstructor
public class UserBillAgentSalaryDiamondCheckExchangeGoldQryExe {

  private final UserRegionClient userRegionClient;
  private final BankRoleCheckCommon bankRoleCheckCommon;
  private final TeamBillCycleClient teamBillCycleClient;

  public UserSalaryDiamondCheckExchangeGoldCO execute(AppIdLongCmd cmd) {

    RegionConfigDTO region = ResponseAssert.requiredSuccess(
        userRegionClient.getSysRegionConfigByUserId(cmd.requiredReqUserId())
    );
    ResponseAssert.notNull(CommonErrorCode.NOT_OPEN_ERROR, region);
    ResponseAssert.notNull(CommonErrorCode.NOT_OPEN_ERROR, region.getMetadata());
    ResponseAssert.isTrue(CommonErrorCode.NOT_OPEN_ERROR,
        Objects.equals(region.getMetadata().get("openExchangeGold"), "true"));

    TeamBillCycleDTO teamBillCycleDTO = ResponseAssert.requiredSuccess(teamBillCycleClient.getBillByTeamId(cmd.getId()));

    return new UserSalaryDiamondCheckExchangeGoldCO()
        .setRole(bankRoleCheckCommon.getExchangeGoldRole(cmd.requiredReqUserId()))
        .setBalance(null);
  }

}
