namespace :ofx do
  desc "Move and rename .ofx files from ~/Downloads/ to db/ofx/"
  task :move, [:account_name] => [:environment] do |t, args|
    if args[:account_name].nil?
      puts "must provide an account name, like `rake ofx:move[laura-chase]`"
      exit
    end
    require "ofx"

    files = Dir.glob(Dir.home+"/Downloads/*.ofx")

    files.each do |f|
      # @no = Nokogiri::Slop(f)
      # ap @no.OFX
      # ap @no.OFX.SIGNONMSGSRSV1
      # @ofx = OFX::Parser::Base.new(f)
      # @parser = @ofx.parser
      # @account = @parser.account
      # ap @ofx.body
      # ap @account

      OFX(f) do

        # @bank_id="321076506", @id="0S10", @type=:checking,
        last = account.transactions.first.posted_at.strftime("%Y%m%d")
        first = account.transactions.last.posted_at.strftime("%Y%m%d")
        new_file = "#{Rails.root.to_s}/db/ofx/#{args[:account_name]}_#{account.bank_id}-#{account.id}_#{first}-#{last}.ofx"
        ap new_file
        # FileUtils.mv(f, new_file)
      end
    end
  end

end
