# frozen_string_literal: true

require_relative "trust_level_repo"

class WelcomeMessage
	def self.for(customer, tel, trust_level_repo: TrustLevelRepo.new)
		trust_level_repo.find(customer).then do |tl|
			new(customer, tel, tl)
		end
	end

	def initialize(customer, tel, trust_level)
		@customer = customer
		@tel = tel
		@trust_level = trust_level
	end

	def welcome
		EM.promise_timer(10).then do
			# Wait for cheogram to get the finish and set up the route
			@customer.stanza_to(message)
		end
	end

	def warning
		return if @trust_level.support_call?(0, 0) && @trust_level.send_message?(0)

		"\n\nYour account is activated for inbound calls and texts, but you " \
		"won't be able to call out or send a text until you receive at least " \
		"one text from a person at your new number, or port in a number from " \
		"outside."
	end

	def message
		m = Blather::Stanza::Message.new
		m.from = CONFIG[:notify_from]
		m.body =
			"Welcome to JMP! Your JMP number is #{@tel}.#{warning}\n\nThis is an " \
			"automated message, but anything you send here will go direct to real " \
			"humans who will be happy to help you.  Such support requests will " \
			"get a reply within 8 business hours.\n\n" \
			"FAQ: https://jmp.chat/faq\n" \
			"Account Settings: xmpp:cheogram.com?command"
		m
	end
end
