# == Schema Information
#
# Table name: article_versions
#
#  id          :integer          not null, primary key
#  created_on  :datetime
#  source_text :text(16777215)
#  title       :string(255)
#  version     :integer          default(0)
#  xml_text    :text(16777215)
#  article_id  :integer
#  user_id     :integer
#
# Indexes
#
#  index_article_versions_on_article_id  (article_id)
#  index_article_versions_on_user_id     (user_id)
#
class ArticleVersion < ApplicationRecord
  belongs_to :article, optional: true
  belongs_to :user, optional: true
  has_many :flags

  after_create :check_content

  def check_content
    Flag.check_article(self)
  end

  def prev
    article.article_versions.where('id < ?', id).first
  end

  def next
    article.article_versions.where('id > ?', id).last
  end

  def current_version?
    self.id == article.article_versions.pluck(:id).max
  end

  def expunge
    # if we are we the current version
    if self.current_version?
      previous_version = self.prev
      if previous_version
        #   copy the previous version's contents into the page and save without callbacks
        article.update_columns(
          title: previous_version.title,
          source_text: previous_version.source_text,
          xml_text: previous_version.xml_text
        )
      else
        article.destroy! # this also deletes the article versions
      end
    else
      #   renumber subsequent versions
      this_version = self
      while next_version = this_version.next do
        next_version.article_version -= 1
        next_version.save!
        this_version = next_version
      end
    end
    self.destroy!
  end
end
