require 'csv'

logger = Logger.new($stdout)
affected_files = [
  '20121029125200_upload_dft_publications.csv',
  '20121029160239_upload_dclg_publications.csv',
  '20121030160045_upload_brac_publications.csv',
  '20121030160102_upload_pins_publications.csv'
].map { |filename| Rails.root.join('db/data_migration/', filename) }

affected_files.each do |affected_file|
  logger.info "Processing file #{affected_file}"
  csv = CSV.read(affected_file, headers: true)
  csv.each_with_index do |data_row, ix|
    row = Whitehall::Uploader::PublicationRow.new(data_row.to_hash, ix + 1, logger)
    begin
      attachment_urls = (1..50).map { |x| data_row["attachment_#{x}_url"] }.compact
      document_source = DocumentSource.find_by_url(row.legacy_url)
      found, missing = attachment_urls.partition { |url|
        AttachmentSource.find_by_url(url)
      }
      if missing.any?
        edition = if found.any?
          AttachmentSource.find_by_url(found.first).attachment.editions.last
        elsif document_source
          document_source.document.latest_edition
        end
        if edition
          logger.warn "Row #{ix + 2} '#{row.legacy_url}' missing attachments (Edition #{edition.id}) - #{missing.inspect}"
        else
          logger.warn "Row #{ix + 2} '#{row.legacy_url}' missing attachments - #{missing.inspect}"
        end
      end
    end
  end
  logger.info "Finished processing file #{affected_file}"
end

Revert "Revert "Check that publication attachments were actually created.""
Revert "Revert "Check that publication attachments were actually created.""

This reverts commit e988be5e0f90d8f5d1c961473b47fafda89061ed.

This was previously reverted as the data migration didn't run and we
didn't want it holding up the deploy + data migration procedure.

I've made a couple of changes to the script as it stood:

* Fixed a problem where the script was trying to use the `url` variable
  but it wasn't in scope at the time it was being used.

* Restricted the set of files we're checking for missing attachments
  to those that we've already imported in production.

* Attempt to find the edition first by going through the attachments and
  then through the DocumentSource.  This gives us 5 additional Edition
  IDs over just using the found attachments to look up the edition.
