class ShopifyApp::Generators::AddWebhookGenerator

def add_webhook_job

def add_webhook_job
  namespace = ShopifyApp.configuration.webhook_jobs_namespace
  @job_file_name = if namespace.present?
    "#{namespace}/#{job_file_name}_job"
  else
    "#{job_file_name}_job"
  end
  @job_class_name = @job_file_name.classify
  template("webhook_job.rb", "app/jobs/#{@job_file_name}.rb")
end

def init_webhook_config

def init_webhook_config
  initializer = load_initializer
  return if initializer.include?("config.webhooks")
  inject_into_file(
    "config/initializers/shopify_app.rb",
    "  config.webhooks = [\n  ]\n",
    after: /ShopifyApp\.configure.*\n/,
  )
end

def inject_webhook_to_shopify_app_initializer

def inject_webhook_to_shopify_app_initializer
  inject_into_file(
    "config/initializers/shopify_app.rb",
    webhook_config,
    after: "config.webhooks = [",
  )
  initializer = load_initializer
  unless initializer.include?(webhook_config)
    shell.say("Error adding webhook to config. Add this line manually: #{webhook_config}", :red)
  end
end

def job_file_name

def job_file_name
  path.split("/").last
end

def load_initializer

def load_initializer
  File.read(File.join(destination_root, "config/initializers/shopify_app.rb"))
end

def path

def path
  options["path"]
end

def topic

def topic
  options["topic"]
end

def webhook_config

def webhook_config
  "\n    { topic: \"#{topic}\", path: \"#{path}\" },"
end