class Ecs::SaveCourseEvaluationForm
  include ActiveModel::Model

  attr_accessor :name, :evaluation_count, :status, :course_evaluation_subitems

  validates :name, presence: true
  validates :evaluation_count, presence: true, numericality: { only_integer: true }
  validates :status, presence: true, inclusion: { in: %w[partly totality] }

  validate :course_evaluation_subitems_validate
  def course_evaluation_subitems_validate
    if course_evaluation_subitems.blank?
      errors.add(:course_evaluation_subitems, :blank)
      return
    end

    if course_evaluation_subitems.any? { |item| item[:name].blank? }
      errors.add(:course_evaluation_subitems, :name_blank)
      return
    end
  end
end
