class MenuItemInstance < ApplicationRecord
  belongs_to :menu_item

  has_many :menu_instance_item_sets
  has_many :item_sets, through: :menu_instance_item_sets
  # before_create :generate_menu_item_instance_code
  # validates_presence_of :item_instance_code

  validates_uniqueness_of :item_instance_code

  def self.findParentCategory(item)
    if item.menu_category_id
      item.menu_category_id
    else
      parentitem = MenuItem.find(item.menu_item_id)
      findParentCategory(parentitem)
    end
  end

  def self.get_item_name(item_code)
    menu_item = MenuItemInstance.find_by_item_instance_code(item_code)
    if menu_item.nil?
      item_name = Product.find_by_item_code(item_code).name
    else
      item_name = menu_item.menu_item.name + ' - ' + menu_item.item_instance_name
    end
    item_name
  end

  # private

  # def generate_menu_item_instance_code
  #   self.item_instance_code = SeedGenerator.generate_code(self.class.name, "II")
  # end
end
