package com.red.circle.wallet.inner.model.dto;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.red.circle.tool.core.num.ArithmeticUtils;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import lombok.Getter;

/**
 * 用户银行卡余额.
 */
@Getter
public class BankBalanceDTO implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 余额.
   */
  @JsonSerialize(using = ToStringSerializer.class)
  private final Long balance;

  /**
   * 转换为精确小数位后的余额.
   */
  private final BigDecimal accurateBalance;

  private BankBalanceDTO(Long balance, BigDecimal accurateBalance) {
    this.balance = balance;
    this.accurateBalance = accurateBalance;
  }

  public static BankBalanceDTO of(Long balance) {
    Long newBalance = balance(balance);
    return new BankBalanceDTO(newBalance,
        BigDecimal.valueOf(newBalance)
            .divide(BigDecimal.valueOf(100), 2, RoundingMode.DOWN)
    );
  }

  private static Long balance(Long balance) {
    if (Objects.isNull(balance)) {
      return 0L;
    }
    return balance;
  }

  /**
   * true 余额不足，false 余额充足.
   */
  public boolean checkInsufficient(BigDecimal amount) {
    return !ArithmeticUtils.gte(accurateBalance, amount);
  }

}
