require 'logger'

namespace :guidance do
  task :import_csv, [:file] => [:environment] do |t, args|
    CSV.foreach(args[:file], {:headers => true}) do |row|
      puts row
    end
  end

  desc "Upload CSVs of Specialist Guidance content to the database"
end
require 'logger'

namespace :guidance do
  task :import_csv, [:file, :topic, :organisation, :creator] => [:environment] do |t, args|
    topic = Topic.where(name: args[:topic]).first
    organisation = Organisation.where(name: args[:organisation]).first
    creator = User.where(email: args[:creator]).first
    unless topic && organisation && creator
      return "Must provide a valid topic, organisation, and creator"
    end

    new_guides = 0
    updated_guides = 0

    CSV.foreach(args[:file], {:headers => true}) do |row|
      title = row[0]
      body = row[1]

      existing_guide = SpecialistGuide.where(title: title).first

      if existing_guide
        existing_guide.body = body
        existing_guide.save && updated_guides += 1
      else
        guide = SpecialistGuide.new(title: title, body: body, state: "draft", topics: [topic], organisations: [organisation], creator: creator)
        guide.save && new_guides += 1
      end
    end
    puts "#{new_guides} created and #{updated_guides} updated"
  end

  desc "Upload CSVs of Specialist Guidance content to the database"
end
Update importer to count rows and use passed in data
Update importer to count rows and use passed in data

Invoke with: rake guidance:import_csv[file,topic_name,org_name,author_email]
