<commit_before>module DataHygiene
  class PolicyPublicationHistoryWriter
    def initialize(publication, policy, logger = NullLogger.instance)
      @publication = publication
      @policy = policy
      @logger = logger
    end

    def rewrite_history!
      Edition::AuditTrail.acting_as(gds_user) do
        logger.info "Backfilling history for: (#{publication.id}) #{publication.title}"
        store_publication_history_and_reset_first_edition
        store_and_reset_archiving
        re_edition_for_major_policy_changes
        replay_publication_history
        re_archive_if_required
        publication.document.reload.change_history.changes.each do |change|
          logger.info "\t(#{change.public_timestamp.to_date.to_s(:uk_short)}) #{change.note}"
        end
      end
    end

  private
    attr_reader :logger, :publication, :policy

    def store_publication_history_and_reset_first_edition
      @publication_history = publication.change_history
      publication.update_column(:change_note, nil)
    end

    def store_and_reset_archiving
      if latest_edition.archived?
        @unpublishing = latest_edition.unpublishing
        latest_edition.update_column(:state, :published)
      end
    end

    def re_archive_if_required
      if @unpublishing
        latest_edition.unpublishing = @unpublishing<commit_msg>Include an editorial remark for auditing<commit_after>module DataHygiene
  class PolicyPublicationHistoryWriter
    def initialize(publication, policy, logger = NullLogger.instance)
      @publication = publication
      @policy = policy
      @logger = logger
    end

    def rewrite_history!
      Edition::AuditTrail.acting_as(gds_user) do
        logger.info "Backfilling history for: (#{publication.id}) #{publication.title}"
        store_publication_history_and_reset_first_edition
        store_and_reset_archiving
        re_edition_for_major_policy_changes
        replay_publication_history
        re_archive_if_required
        add_an_editorial_remark
        publication.document.reload.change_history.changes.each do |change|
          logger.info "\t(#{change.public_timestamp.to_date.to_s(:uk_short)}) #{change.note}"
        end
      end
    end

  private
    attr_reader :logger, :publication, :policy

    def store_publication_history_and_reset_first_edition
      @publication_history = publication.change_history
      publication.update_column(:change_note, nil)
    end

    def store_and_reset_archiving
      if latest_edition.archived?
        @unpublishing = latest_edition.unpublishing
        latest_edition.update_column(:state, :published)
      end
    end

    def add_an_editorial_remark
      latest_edition.editorial_remarks.create(body: "Rewrote document history to match original publication", author: gds_user)
    end

    def re_archive_if_required
      if @unpublishing
        latest_edition.unpublishing = @unpublishing
{'fraction_non_alphanumeric': 0.06426116838487972, 'fraction_numerical': 0.0, 'mean_word_length': 3.471582181259601, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2976238', 'n_tokens_mistral': 854, 'n_tokens_neox': 838, 'n_words': 154}