# frozen_string_literal: true

require "forwardable"

require_relative "./blather_ext"
require_relative "./customer_usage"
require_relative "./customer_plan"
require_relative "./customer_ogm"
require_relative "./customer_financials"
require_relative "./backend_sgx"
require_relative "./invites_repo"
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, :feature_flags
	alias billing_customer_id customer_id

	def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan,
	               :currency, :merchant_account, :plan_name, :minute_limit,
	               :message_limit, :monthly_overage_limit, :activation_date,
	               :expires_at, :monthly_price, :save_plan!, :auto_top_up_amount,
	               :extend_plan, :status
	def_delegators :@sgx, :deregister!, :register!, :registered?, :set_ogm_url,
	               :fwd, :transcription_enabled, :set_port_out_pin, :tn_portable?
	def_delegators :@usage, :usage_report, :message_usage, :incr_message_usage,
	               :calling_charges_this_month
	def_delegators :@financials, :payment_methods, :declines, :mark_decline,
	               :btc_addresses, :add_btc_address, :transactions,
	               :bch_addresses, :add_bch_address

	def self.extract(customer_id, jid, **kwargs)
		(kwargs[:parent_customer_id] ? ChildCustomer : Customer).new(
			customer_id, jid,
			plan: CustomerPlan.extract(customer_id: customer_id, **kwargs),
			**kwargs.slice(:balance, :sgx, :tndetails, :feature_flags)
		)
	end

	def self.created(customer_id, jid, repo:, **kwargs)
		jid = Blather::JID.new(jid)
		plan = CustomerPlan.default(customer_id, jid)
		if plan.parent_customer_id
			# Parent may have a balance, so look it up
			plan.save_plan!.then { repo.find(customer_id) }
		else
			new(customer_id, jid, plan: plan, **kwargs)
		end
	end

	def initialize(
		customer_id,
		jid,
		sgx:,
		plan: CustomerPlan.new(customer_id),
		balance: BigDecimal(0),
		tndetails: {},
		feature_flags: []
	)
		@plan = plan
		@usage = CustomerUsage.new(customer_id)
		@financials = CustomerFinancials.new(customer_id)
		@customer_id = customer_id
		@jid = jid
		@balance = balance
		@tndetails = tndetails
		@feature_flags = feature_flags
		@sgx = sgx
	end

	def with_balance(balance)
		self.class.new(
			@customer_id, @jid,
			plan: @plan, balance: balance,
			tndetails: @tndetails, sgx: @sgx
		)
	end

	def with_plan(plan_name, **kwargs)
		self.class.new(
			@customer_id, @jid,
			plan: @plan.with(plan_name: plan_name, **kwargs),
			balance: @balance, tndetails: @tndetails, sgx: @sgx
		)
	end

	def billing_customer(*)
		EMPromise.resolve(self)
	end

	def stanza_to(stanza)
		stanza = stanza.dup
		stanza.to = jid.with(resource: stanza.to&.resource)
		stanza.from ||= Blather::JID.new("")
		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_pep(node, from_tel=nil)
		from_tel = nil unless from_tel.to_s.start_with?("+")
		iq = Blather::Stanza::PubSub::Items.new(:get)
		iq.node = node
		iq.from = Blather::JID.new(from_tel, CONFIG[:component][:jid])
		stanza_to(iq, &IQ_MANAGER.method(:write))
	end

	def ogm(from_tel=nil)
		fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) }
		CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard)
	end

	def sip_account
		SipAccount.find(customer_id)
	end

	def sgx
		@sgx.jid
	end

	def reset_sip_account
		sip_account.with_random_password.put
	end

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

	class ChildCustomer < Customer
		def billing_customer_id
			@plan.parent_customer_id
		end

		def billing_customer(repo=CustomerRepo.new)
			repo.find(billing_customer_id)
		end
	end
end
