class MenuItem < ApplicationRecord
  # before_create :generate_menu_item_code

  belongs_to :menu_category, :optional => true
  has_many :menu_item_instances
  has_many :commissions
  has_many :product_commissions

  # belongs_to :parent, :class_name => "MenuItem", foreign_key: "menu_item_id", :optional => true
  # has_many :children, :class_name => "MenuItem", foreign_key: "menu_item_id"
  belongs_to :account

  has_many :menu_item_sets
  has_and_belongs_to_many :item_sets, join_table: "menu_item_sets"

  validates_presence_of :item_code, :name, :type, :min_qty,:account_id
  validates_uniqueness_of :item_code, scope: :shop_code

  default_scope { order('item_code asc') }

  scope :simple_menu_item, -> { where(type: 'SimpleMenuItem') }
  scope :set_menu_item, -> { where(type: 'SetMenuItem') }
  scope :active, -> { where(is_available: true) }

  # Item Image Uploader
  mount_uploader :image_path, MenuItemImageUploader

  def self.collection
    MenuItem.select("id, name").map { |e| [e.name, e.id] }
  end

  # Work with item_code = item_instance_code
  def self.search_by_item_code(item_code)
    MenuItem.left_joins(:menu_item_instances => :menu_instance_item_sets)
    .where(menu_item_instances: {item_instance_code: item_code})
    .pluck(:type, :account_id, :item_code, :item_instance_code, :name, :alt_name, :item_instance_name, :price, :promotion_price, :is_on_promotion, "menu_item_instances.is_available", :taxable, :item_set_id)
    .map { |type, account_id, item_code, item_instance_code, item_name, item_alt_name, item_instance_name, price, promotion_price, is_on_promotion, is_available, taxable, item_set_id|
      name = item_instance_name if item_set_id
      name ||= "#{item_name}#{' - ' + item_instance_name if item_instance_name.present?}"
      {
        type: type,
        account_id: account_id,
        item_code: item_code,
        item_instance_code: item_instance_code,
        name: "#{name}",
        alt_name: "#{item_alt_name}",
        price: price,
        promotion_price: promotion_price,
        is_on_promotion: is_on_promotion,
        is_available: is_available,
        taxable: taxable
      }
    }
  end

  def self.deleteRecursive(menu_item)
    # find the sub menu item of current item
    # sub_menu_items = MenuItem.where("id=?",menu_item.id)
    # if sub_menu_items.length != 0
    #   # sub_menu_items.each do |subitem|
    #   #   # if deleteRecursive(subitem)
    #   #   # end
    #   # end
    #   # find the instances of current menu item
    #   instances = MenuItemInstance.where("menu_item_id=?",menu_item.id)
    #   instances.each do |instance|
    #     instance.menu_instance_item_sets.destroy_all
    #   end
    #   menu_item.menu_item_sets.destroy_all
    #   return true
    # else
    #   instances = MenuItemInstance.where("menu_item_id=?",menu_item.id)
    #   instances.each do |instance|
    #     instance.menu_instance_item_sets.destroy_all
    #   end
    #   menu_item.menu_item_sets.destroy_all
    #   return true
    # end
    instances = MenuItemInstance.where("menu_item_id=?",menu_item.id)
    instances.each do |instance|
      instance.menu_instance_item_sets.destroy_all
      instance.destroy
    end
    menu_item.menu_item_sets.destroy_all
    menu_item.destroy
    return true

  end
  # private

  # def generate_menu_item_code
  #   self.item_code = SeedGenerator.generate_code(self.class.name, "I")
  # end

end
