# frozen_string_literal: true

class Report < ApplicationRecord
  has_many :pages, -> { order(:position) }, dependent: :destroy
  has_many :elements, through: :pages, dependent: :destroy
  has_many :success_criteria, through: :elements, dependent: :destroy

  has_rich_text :comment

  validates :name, presence: true

  def export
    export_success_criteria = success_criteria.failed
    export_elements = export_success_criteria.group_by(&:element)
    export_pages = export_elements.group_by { |k, v| k }
    {
      pages: export_pages,
      elements: export_elements,
      success_criteria: export_success_criteria
    }
  end
end
