class FoodSerializer < ApplicationSerializer

  def serialize
    {
        **item_properties(:id,
                          :name,
                          :ndbn,
                          :usda_food_name,
                          :notes,
                          :density,
                          :water,
                          :ash,
                          :protein,
                          :kcal,
                          :fiber,
                          :sugar,
                          :carbohydrates,
                          :calcium,
                          :iron,
                          :magnesium,
                          :phosphorus,
                          :potassium,
                          :sodium,
                          :zinc,
                          :copper,
                          :manganese,
                          :vit_c,
                          :vit_b6,
                          :vit_b12,
                          :vit_a,
                          :vit_e,
                          :vit_d,
                          :vit_k,
                          :cholesterol,
                          :lipids),

        ndbn_units: self.ndbn_units,
        food_units: item.food_units.map { |u| { id: u.id, name: u.name, gram_weight: u.gram_weight, _destroy: false } }
    }
  end

  def ndbn_units
    if item.ndbn.present?
      item.usda_food.usda_food_weights.map do |fw|
        {
          amount: fw.amount,
          description: fw.description,
          gram_weight: fw.gram_weight
        }
      end
    else
      []
    end
  end

end