# frozen_string_literal: true

class EditSimNicknames
	# @param [Customer] customer the customer editing the sims
	# @param [Array<Sim>] sims the sims whose nicknames
	# 					  are up for editing
	def initialize(customer, sims, sim_repo: SIMRepo.new(db: DB))
		@customer = customer
		@sims = sims
		@sim_repo = sim_repo
	end

	def form
		FormTemplate.render(
			"edit_sim_nicknames",
			sims: @sims
		)
	end

	def complete(iq)
		EMPromise.resolve(nil).then {
			sims = @sims.map { |sim|
				sim.with(nickname: iq.form.field("sim-#{sim.iccid}").value)
			}
			@sim_repo.update_nicknames(sims)
		}.then {
			Command.finish("Successfully updated SIM nicknames.")
		}
	end
end
