# frozen_string_literal: true

require "forwardable"
require "ruby-bandwidth-iris"
Faraday.default_adapter = :em_synchrony

class BandwidthTnReservationRepo
	FOUR_HOURS = 60 * 60 * 4

	def initialize(redis: REDIS)
		@redis = redis
	end

	def ensure(customer, tel)
		REDIS.exists(key(customer, tel)).then do |exists|
			unless exists == 1
				reservation_id = BandwidthIris::TnReservation.create(tel).reservation_id
				REDIS.setex(key(customer, tel), FOUR_HOURS, reservation_id)
			end
		end
	end

	def get(customer, tel)
		REDIS.get(key(customer, tel))
	end

	def key(customer, tel)
		"jmp_customer_tn_reservation_#{tel}-#{customer.customer_id}"
	end
end
