module M2yBiz
  module Wallet
    class Recharge < Base

      def initialize(token)
        @headers = json_headers(true, token)
      end

      def newPhoneRecharge(body)
        # https://code.biz.com.br/api-details#api=wallet-lancamentos&operation=post-entries-marketplace-products-type-buy
        return Base::FAILURE if body.blank?
        url = "#{base_url}#{Constants::Paths::RECHARGE_PHONE_PATH}"
        request = HTTParty.post(url, body: body.to_json, headers: @headers)
        BizHelper.parse_response(request)
      end

      def listAvailableProducts
        # https://code.biz.com.br/api-details#api=wallet-lancamentos&operation=get-entries-marketplace-products-type
        url = "#{base_url}#{Constants::Paths::LIST_PHONE_RECHARGE_PRODUCTS_PATH}"
        request = HTTParty.get(url, headers: @headers)
        BizHelper.parse_response(request)
      end

      def listAvailablePrices(product, areaCode)
        # https://code.biz.com.br/api-details#api=wallet-lancamentos&operation=get-entries-marketplace-products-type-optionname-areacode-areacode
        return Base::FAILURE if product.blank? || areaCode.blank?
        url = "#{base_url}#{Constants::Paths::LIST_PHONE_RECHARGE_VALUES_PATH}/#{product}?areaCode=#{areaCode}"
        request = HTTParty.get(url, headers: @headers)
        BizHelper.parse_response(request)
      end

    end
  end
end
