# frozen_string_literal: true

require "bigdecimal"

module SIMPricing
	# SIM refill billing records currency amounts in transactions. Use BigDecimal
	# because the configured per-GB price is stored in cents.
	def self.per_gb_price(currency, config)
		configured_price_dollars(:per_gb, currency, config)
	end

	def self.annual_price(currency, config)
		configured_price_dollars(:annual, currency, config)
	end

	def self.configured_price_dollars(price_type, currency, config)
		BigDecimal(
			config.fetch(:sims).fetch(price_type).fetch(currency)
		) / 100
	end
	private_class_method :configured_price_dollars

	def self.five_gb_refill_price(currency, config)
		per_gb_price(currency, config) * 5
	end

	def self.per_gb_label(currency, config)
		"$%.2f / GB" % per_gb_price(currency, config)
	end

	def self.plan_label(currency, config)
		"$%.2f / GB + $%.2f / year" % [
			per_gb_price(currency, config),
			annual_price(currency, config)
		]
	end

	# CustomerRepo#put_monthly_limits stores limits with to_i, so render monthly
	# data limit form steps in that same integer-dollar unit.
	def self.whole_dollars(price)
		price.to_i
	end
end
