# frozen_string_literal: true

class AdminAction
	class LaunchSnikket
		def self.call(customer_id, reply:, snikket_repo:, **)
			reply.call(
				FormTemplate.render("snikket_launch")
			).then { |response|
				domain = response.form.field("domain").value.to_s
				IQ_MANAGER.write(Snikket::Launch.new(
					nil, CONFIG[:snikket_hosting_api], domain: domain
				)).then do |launched|
					Snikket::CustomerInstance.for(customer_id, domain, launched)
				end
			}.then { |instance| save(snikket_repo, reply, instance) }
		end

		def self.save(snikket_repo, reply, instance)
			snikket_repo.put(instance).then do
				reply.call(FormTemplate.render(
					"snikket_launched",
					instance: instance
				))
			end
		end
	end
end
