# frozen_string_literal: true

require "forwardable"

require_relative "./api"
require_relative "./blather_ext"
require_relative "./customer_info"
require_relative "./customer_ogm"
require_relative "./customer_plan"
require_relative "./customer_usage"
require_relative "./backend_sgx"
require_relative "./ibr"
require_relative "./payment_methods"
require_relative "./plan"
require_relative "./proxied_jid"
require_relative "./sip_account"
require_relative "./trivial_backend_sgx_repo"

class Customer
	extend Forwardable

	attr_reader :customer_id, :balance, :jid, :tndetails

	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
	               :currency, :merchant_account, :plan_name, :minute_limit,
	               :message_limit, :auto_top_up_amount, :monthly_overage_limit
	def_delegators :@sgx, :register!, :registered?, :set_ogm_url,
	               :fwd, :transcription_enabled
	def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage

	def initialize(
		customer_id,
		jid,
		plan: CustomerPlan.new(customer_id),
		balance: BigDecimal(0),
		tndetails: {},
		sgx: TrivialBackendSgxRepo.new.get(customer_id)
	)
		@plan = plan
		@usage = CustomerUsage.new(customer_id)
		@customer_id = customer_id
		@jid = jid
		@balance = balance
		@tndetails = tndetails
		@sgx = sgx
	end

	def with_plan(plan_name)
		self.class.new(
			@customer_id,
			@jid,
			plan: @plan.with_plan_name(plan_name),
			balance: @balance,
			sgx: @sgx
		)
	end

	def payment_methods
		BRAINTREE
			.customer
			.find(@customer_id)
			.catch { OpenStruct.new(payment_methods: []) }
			.then(PaymentMethods.method(:for_braintree_customer))
	end

	def unused_invites
		promise = DB.query_defer(<<~SQL, [customer_id])
			SELECT code FROM unused_invites WHERE creator_id=$1
		SQL
		promise.then { |result| result.map { |row| row["code"] } }
	end

	def stanza_to(stanza)
		stanza = stanza.dup
		stanza.to = jid.with(resource: stanza.to&.resource)
		stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
		block_given? ? yield(stanza) : (BLATHER << stanza)
	end

	def stanza_from(stanza)
		BLATHER << @sgx.stanza(stanza)
	end

	def fetch_vcard_temp(from_tel=nil)
		iq = Blather::Stanza::Iq::Vcard.new(:get)
		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
		stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
	end

	def ogm(from_tel=nil)
		CustomerOGM.for(@sgx.ogm_url, -> { fetch_vcard_temp(from_tel) })
	end

	def sip_account
		SipAccount.find(customer_id)
	end

	def reset_sip_account
		sip_account.with_random_password.put
	end

	def btc_addresses
		REDIS.smembers("jmp_customer_btc_addresses-#{customer_id}")
	end

	def add_btc_address
		REDIS.spopsadd([
			"jmp_available_btc_addresses",
			"jmp_customer_btc_addresses-#{customer_id}"
		]).then do |addr|
			ELECTRUM.notify(
				addr,
				CONFIG[:electrum_notify_url].call(addr, customer_id)
			)
			addr
		end
	end

	def admin?
		CONFIG[:admins].include?(jid.to_s)
	end

	def api
		API.for(self)
	end

	def admin_info
		AdminInfo.for(self, @plan)
	end

	def info
		CustomerInfo.for(self, @plan)
	end

	protected def_delegator :@plan, :expires_at
end
