class PostVersionRecorder < VersionRecorder
  def self.record! post:, event_type:, created_by_user:
    new(post:, event_type:, created_by_user:).record!
  end

  def initialize post:, event_type:, created_by_user:
    super(record: post, event_type:, created_by_user:)
  end

  def self.ensure_snapshot! post, created_by_user:
    return if post.post_versions.exists?

    record!(post:, event_type: :create, created_by_user:)
  end

  private

  def version_class = PostVersion
  def version_association = :post_versions
  def record_key = :post

  def snapshot_attributes
    { title: @record.title,
      url: @record.url,
      thumbnail_base: @record.thumbnail_base,
      video_ms: @record.video_ms,
      tags: @record.snapshot_tag_names.join(' '),
      tags_json: @record.snapshot_tags_json,
      parent_post_ids: @record.snapshot_parent_post_ids.join(' '),
      original_created_from: @record.original_created_from,
      original_created_before: @record.original_created_before }
  end
end
