if Rails.env.development?
  Rails.logger = Logger.new(STDOUT)
end

require 'factory_girl'
require 'timecop'
require 'rspec-rails'
begin
require Rails.root.join('spec', 'support', 'lessons.rb')
rescue LoadError
  puts "for production; we ignore LoadError"
rescue TypeError
  puts "for production, we ignore type error"
end

def generate
  [*('a'..'z'),*('0'..'9')].shuffle[0,10].join.upcase
end

def gc_10
  CSV.open("gift-card-10.csv", "wb") do |csv|
    for i in  1..150
      csv << [generate()]
    end
  end
end

def gc_20
  CSV.open("gift-card-20.csv", "wb") do |csv|
    for i in  1..100
      csv << [generate()]
    end
  end
end


# round to.  we make

#One set of 200 codes that when redeemed translate into 5 (not 10) JamTracks each.

#One set of 200 codes that when redeemed translate into 4 JamClass lessons each.

def gc_5jt_2
  CSV.open("posa-cards-jt-5.csv", "wb") do |csv|
    for i in  1..250
      csv << [generate()]
    end
  end
end

def gc_4jc_2
  CSV.open("posa-cards-jc-4.csv", "wb") do |csv|
    for i in  1..250
      csv << [generate()]
    end
  end
end

namespace :lessons do

  task amazon_gift_cards: :environment do|task, args|
    #CSV.open("posa-cards-amazon-test-drive-paid-4-2nd.csv", "wb") do |csv|
    #  for i in  1..50000
    #    csv << [generate(), 'amazon-test-drive-paid-4', true, true, false]
    #  end
    #end

    #CSV.open("posa-cards-amazon-test-drive-free-4-2nd.csv", "wb") do |csv|
    #  for i in  1..50000
    #    csv << [generate(), 'amazon-test-drive-free-4',true, false, false]
    #  end
    #end

    CSV.open("posa-cards-amazon-test-drive-free-2-2nd.csv", "wb") do |csv|
      for i in  1..50000
        csv << [generate(), 'amazon-test-drive-free-2',true, false, false]
      end
    end
  end
  task book_completed: :environment do |task, args|

    user = User.find_by_email(ENV['STUDENT'])
    teacher = User.find_by_email(ENV['TEACHER'])
    recurring = ENV['RECURRING'] == '1'
    slots = []
    Timecop.travel(Date.today - 5)
    slots << FactoryGirl.build(:lesson_booking_slot_single, preferred_day: Date.today - 3, timezone: 'America/Chicago')
    slots << FactoryGirl.build(:lesson_booking_slot_single, preferred_day: Date.today - 4, timezone: 'America/Chicago')
    if recurring
      payment_style = LessonBooking::PAYMENT_STYLE_MONTHLY
    else
      payment_style = LessonBooking::PAYMENT_STYLE_SINGLE
    end

    lesson = normal_lesson(user, teacher, {accept: true, finish: true, student_show:true, no_validate: true})


    if lesson.errors.any?
      puts lesson.errors.inspect
      raise "lesson failed"
    end

    puts "http://localhost:3000/client#/jamclass/lesson-booking/#{lesson.id}"
  end
  task book_normal: :environment do |task, args|
    user = User.find_by_email(ENV['STUDENT'])
    teacher = User.find_by_email(ENV['TEACHER'])
    recurring = ENV['RECURRING'] == '1'
    slots = []

    if recurring
      slots << FactoryGirl.build(:lesson_booking_slot_recurring, timezone: 'America/Chicago')
      slots << FactoryGirl.build(:lesson_booking_slot_recurring, timezone: 'America/Chicago')
    else
      slots << FactoryGirl.build(:lesson_booking_slot_single, timezone: 'America/Chicago')
      slots << FactoryGirl.build(:lesson_booking_slot_single, timezone: 'America/Chicago')

    end


    if user.stored_credit_card == false
      user.stored_credit_card = true
      user.save!
    end

    if recurring
      payment_style = LessonBooking::PAYMENT_STYLE_MONTHLY
    else
      payment_style = LessonBooking::PAYMENT_STYLE_SINGLE
    end


    booking = LessonBooking.book_normal(user, teacher, slots, "Hey I've heard of you before.", recurring, payment_style, 30)
    if booking.errors.any?
      puts booking.errors.inspect
      raise "booking failed"
    end
    lesson = booking.lesson_sessions[0]


    #lesson.accept({message: 'Yeah I got this', slot: slots[0]})
    #lesson.errors.any?.should be false
    #lesson.reload
    #lesson.slot.should eql slots[0]
    #lesson.status.should eql LessonSession::STATUS_APPROVED

    puts "http://localhost:3000/client#/jamclass/lesson-booking/#{booking.id}"
  end

  task book_test_drive: :environment do |task, args|
    user = User.find_by_email(ENV['STUDENT'])
    teacher = User.find_by_email(ENV['TEACHER'])


    slots = []

    slots << FactoryGirl.build(:lesson_booking_slot_single, timezone: 'America/Chicago')
    slots << FactoryGirl.build(:lesson_booking_slot_single, timezone: 'America/Chicago')

    if user.stored_credit_card == false
      user.stored_credit_card = true
      user.save!
    end

    booking = LessonBooking.book_test_drive(user, teacher, slots, "Hey I've heard of you before.")
    if booking.errors.any?
      puts booking.errors.inspect
      raise "booking failed"
    end
    lesson = booking.lesson_sessions[0]

    if user.most_recent_test_drive_purchase.nil?
      LessonPackagePurchase.create(user, lesson.lesson_booking, LessonPackageType.test_drive_4)
    end

    #lesson.accept({message: 'Yeah I got this', slot: slots[0]})
    #lesson.errors.any?.should be false
    #lesson.reload
    #lesson.slot.should eql slots[0]
    #lesson.status.should eql LessonSession::STATUS_APPROVED

    puts "http://localhost:3000/client#/jamclass/lesson-booking/#{lesson.lesson_booking.id}"
  end
end
