#
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
#
class BtcPay
  def initialize
    @base_url = ENV["BTCPAY_API_URL"]
    @store_id = Rails.application.credentials.btcpay[:store_id]
    @auth_token = Rails.application.credentials.btcpay[:auth_token]
  end

  def onchain_wallet_balance
    res = get "stores/#{@store_id}/payment-methods/onchain/BTC/wallet"

    {
      balance: res["balance"].to_f,
      unconfirmed_balance: res["unconfirmedBalance"].to_f,
      confirmed_balance: res["confirmedBalance"].to_f
    }
  end

  private

  def get(endpoint)
    res = Faraday.get("#{@base_url}/#{endpoint}", {}, {
      "Content-Type" => "application/json",
      "Accept" => "application/json",
      "Authorization" => "token #{@auth_token}"
    })

    JSON.parse(res.body)
  end
end
