namespace :usda do

  task :list_branded_nutrients do
    require 'csv'

    NutrientCollector = Struct.new(:name, :total_count, :unit_frequencies) do
      def record(name, unit)
        self.name = name
        self.total_count = (self.total_count || 0) + 1
        self.unit_frequencies ||= Hash.new { |h, k| h[k] = 0 }
        self.unit_frequencies[unit] += 1
      end
    end

    totals = Hash.new { |h, k| h[k] = NutrientCollector.new() }

    f = CSV.open(Rails.root.join('vendor', 'data', 'usda_branded', 'Nutrients.csv'), 'r:iso-8859-1:utf-8')
    f.each do |row|
      code = row[1]
      name = row[2]
      unit = row[5]
      totals[code].record(name, unit)
    end

    totals.each do |k, v|
      puts "#{k}, #{v.name}: #{v.total_count}    #{v.unit_frequencies.map { |k,v| "#{k}: #{v}" }.join('; ')}"
    end

  end

  desc 'Empties usda_foods table, imports all data, and then updates any linked ingredients'
  task import: :environment do
    require 'usda_importer'
    importer = UsdaImporter.new(Rails.root.join('vendor', 'data', 'usda'))
    importer.import
  end
end