class UpdateTranscriptSectionsId < ActiveRecord::Migration[7.0]
  def up
    RespondentCategory.includes(:respondents, :master_sections, :transcript_sections).find_each do |category|
      master_sections_map = category.master_sections.index_by(&:name)
      category.respondents.each do |respondent|
        respondent.transcript_sections.each do |transcript_section|
          correct_section = master_sections_map[transcript_section.name]
          if correct_section
            transcript_section.update!(master_section_id: correct_section.id)
          else
            Rails.logger.error "No matching MasterSection for TranscriptSection #{transcript_section.id}"
          end
        end
      end
    end
  end

  def down
    # You can define the rollback logic here if necessary.
    raise ActiveRecord::IrreversibleMigration
  end
end
