# frozen_string_literal: true

require "redis"

class RedisAddresses
	def initialize(redis, currency)
		@redis = redis
		@currency = currency.downcase
	end

	def each_user
		# I picked 1000 because it made a relatively trivial case take
		# 15 seconds instead of forever.
		# Basically it's "how long does each command take"
		# The lower it is (default is 10), it will go back and forth
		# to the client a ton
		@redis.scan_each(
			match: "jmp_customer_#{@currency}_addresses-*",
			count: 1000
		) do |key|
			yield key, @redis.smembers(key)
		end
	end

	# This returns a hash
	# The keys are the bitcoin addresses, the values are all of the keys which
	#   contain that address
	# If there are no duplicates, then each value will be a singleton list
	def get_addresses_with_users
		addrs = Hash.new { |h, k| h[k] = [] }

		# 1000 because it made a relatively trivial case take 15 seconds
		# instead of forever.
		# Basically it's "how long does each command take"
		# The lower it is (default is 10), it will go back and forth
		# to the client a ton
		pattern = "jmp_customer_#{@currency}_addresses-*"
		@redis.scan_each(match: pattern, count: 1000) do |key|
			@redis.smembers(key).each do |addr|
				addrs[addr] << key
			end
		end

		addrs
	end
end
