# frozen_string_literal: true

require "blather"
require "blather/stanza/iq/command"
require "value_semantics/monkey_patched"

require_relative "customer_fwd"
require_relative "not_loaded"
require_relative "form_to_h"

class BackendSgx
	class CanceledError < StandardError; end

	using FormToH

	value_semantics do
		jid Blather::JID
		creds HashOf(Symbol => String)
		from_jid Blather::JID
		ogm_url Either(String, nil, NotLoaded)
		fwd Either(CustomerFwd, nil, NotLoaded)
		transcription_enabled Either(Bool(), NotLoaded)
		registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded)
	end

	def register!(tel)
		iq = ibr
		iq.phone = tel
		IQ_MANAGER.write(iq).then do
			REDIS.set("catapult_jid-#{tel}", from_jid.to_s)
		end
	end

	def deregister!
		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
		ibr.from = from_jid
		ibr.remove!
		IQ_MANAGER.write(ibr)
	end

	def stanza(s)
		s.dup.tap do |stanza|
			stanza.to = stanza.to.with(
				domain: jid.domain,
				node: jid.node || stanza.to.node
			)
			stanza.from = from_jid.with(resource: stanza.from&.resource)
		end
	end

	def set_ogm_url(url)
		REDIS.set("catapult_ogm_url-#{from_jid}", url)
	end

	def set_port_out_pin(pin)
		cmd = build_port_out_command(:execute)

		IQ_MANAGER.write(cmd).then { |reply|
			session_id = reply.command[:sessionid]
			submit_cmd = build_submit_form(pin, session_id)

			IQ_MANAGER.write(submit_cmd).then { |submit_reply|
				validate_submit_reply!(submit_reply)
			}.catch { |e|
				handle_pin_submission_error(e)
			}
		}
	end

protected

	def ibr
		s = Blather::Stanza::Iq::IBR.new(:set, jid)
		s.from = from_jid
		s.nick = creds[:account]
		s.username = creds[:username]
		s.password = creds[:password]
		s
	end

	def build_submit_form(pin, session_id)
		build_port_out_command(:complete, session_id: session_id).tap { |iq|
			iq.form.type = :submit
			iq.form.fields = [
				{ var: "pin", value: pin, type: "text-private" },
				{ var: "confirm_pin", value: pin, type: "text-private" }
			]
		}
	end

	def build_port_out_command(action, session_id: nil)
		Blather::Stanza::Iq::Command.new.tap { |iq|
			iq.to = jid
			iq.from = from_jid
			iq.node = "set-port-out-pin"
			iq.action = action
			iq.sessionid = session_id if session_id
		}
	end

	def validate_submit_reply!(submit_reply)
		sub_text = submit_reply.note&.text
		case submit_reply.status
		when :completed
			raise sub_text if submit_reply.note&.[]("type") == "error"
		when :canceled
			raise CanceledError, reply.note&.text
		else
			raise sub_text
		end
	end

	def handle_pin_submission_error(e)
		if e.is_a?(Blather::StanzaError) || e.is_a?(RuntimeError)
			EMPromise.reject(e)
		else
			Sentry.capture_exception(e)
			EMPromise.reject(
				RuntimeError.new(
					"Unable to communicate with service. Please try again later."
				)
			)
		end
	end
end
