require "data_hygiene/content_consistency_checker"

def report_errors(errors, content_store)
  Airbrake.notify(
    "Documents inconsistent with the #{content_store} content store",
    parameters: {
      errors: errors,
    }
  )

  errors.each do |base_path, item_errors|
    puts "#{base_path} 😱"
    puts item_errors
  end
end

desc "Check all the documents for consistency with the content-store"
task :check_content_consistency, [:content_store, :content_dump] => [:environment] do |_, args|
  raise "Missing content store." unless args[:content_store]
  raise "Invalid content store." unless %w(live draft).include?(args[:content_store])
  raise "Missing content dump." unless args[:content_dump]

  content_dump = ContentDumpLoader.load(args[:content_dump])

  checker = DataHygiene::ContentConsistencyChecker.new(args[:content_store], content_dump)
  checker.check_editions
  checker.check_content

  errors = checker.errors
  report_errors(errors, args[:content_store]) if errors.any?
end

require "data_hygiene/content_consistency_checker"

def report_errors(errors, content_store)
  Airbrake.notify_sync(
    "Documents inconsistent with the #{content_store} content store",
    parameters: {
      errors: errors,
    }
  )

  errors.each do |base_path, item_errors|
    puts "#{base_path} 😱"
    puts item_errors
  end
end

desc "Check all the documents for consistency with the content-store"
task :check_content_consistency, [:content_store, :content_dump] => [:environment] do |_, args|
  raise "Missing content store." unless args[:content_store]
  raise "Invalid content store." unless %w(live draft).include?(args[:content_store])
  raise "Missing content dump." unless args[:content_dump]

  content_dump = ContentDumpLoader.load(args[:content_dump])

  checker = DataHygiene::ContentConsistencyChecker.new(args[:content_store], content_dump)
  checker.check_editions
  checker.check_content

  errors = checker.errors
  report_errors(errors, args[:content_store]) if errors.any?
end

Use Airbrake.notify_sync instead of Airbrake.notify
Use Airbrake.notify_sync instead of Airbrake.notify

When running in a rake task, Airbrake.notify doesn't properly
finish triggering before the rake task ends and therefore the
message never makes it to Airbrake.
