# Following decommissioning of manuals-frontend we are improving simplicity of rendering
# these manuals in government-frontend by adding the manual title to the manual sections
# in the publishing_api via this temporary Rake task. It will only update
# sections of editions that are currently live in the content-store,
# drafts will be updated when published from the API.

def update_hmrc_manual_section_titles(dry_run: false)
  hmrc_manual_sections = Document.presented.where(editions: { publishing_app: "hmrc-manuals-api", document_type: "hmrc_manual_section" })

  hmrc_manual_sections.each do |document|
    edition = document.live

    next if edition.blank?

    details = edition.details
    manual_edition = Edition.live.where(base_path: details[:manual][:base_path]).first

    if manual_edition
      details[:manual][:title] = manual_edition.title
      puts("Updating HMRC Manual Section #{document.content_id} to set manual title to #{details[:manual][:title]}")
      unless dry_run
        edition.update!(details: details)
        Commands::V2::RepresentDownstream.new.call(document.content_id)
      end
    else
      puts("Error: no parent manual found for HMRC Manual Section #{document.content_id}")
    end
  end
end

namespace :tmp_update_hmrc_manual_sections do
  desc "Show hmrc_manual_section items that will be updated with the title of the containing manual"
  task dry_run: :environment do
    puts("DRY RUN (no actual changes will be saved)")
    update_hmrc_manual_section_titles(dry_run: true)
  end

  desc "Update hmrc_manual_section items with the title of the containing manual"
  task go: :environment do
    update_hmrc_manual_section_titles
  end
end

Add temporary rake task to update titles on hmrc_manual_Section items.
Add temporary rake task to update titles on hmrc_manual_Section items.

To simplify front-end rendering, we need to add the parent manual title
to hmrc manual section items.

This commit adds a task to update the values on existing records (can be
removed once this job is complete and all existing and new
hmrc_manual_sections will have the title already).
