class ExaminationItem < ApplicationRecord
  enum item_type: { SINGLE: 0, MULTIPLE: 1, JUDGMENT: 2, COMPLETION: 3, SUBJECTIVE: 4, PRACTICAL: 5, PROGRAM: 6 }

  belongs_to :examination_bank, touch: true
  belongs_to :item_bank, optional: true

  has_many :examination_item_choices, dependent: :destroy
  has_one :examination_item_analysis, dependent: :destroy
  belongs_to :container, polymorphic: true, optional: true

  def analysis
    examination_item_analysis&.analysis
  end

  def item_choices
    examination_item_choices
  end

  def public
    0
  end

  def quotes
    0
  end

  # 题库复制
  def new_item item, exam, score, position
    attributes = {examination_bank: exam, item_bank: item, name: item.name, item_type: item.item_type,
                  difficulty: item.difficulty, score: score, position: position}
    self.update!(attributes)
    item.increment!(:quotes)

    if item.item_type == "PROGRAM"
      new_hack = item.container.fork
      self.update!(container: new_hack)
    else
      ExaminationItemAnalysis.create!(analysis: item.analysis, examination_item_id: self.id)
      item.item_choices.each do |choice|
        examination_item_choices << ExaminationItemChoice.new(choice_text: choice.choice_text, is_answer: choice.is_answer)
      end
    end
  end

end
