namespace :panda_pal do

desc 'Cleans up old session data'
task clean_sessions: :environment do
  PandaPal::Session.where('updated_at < ?', 1.week.ago).delete_all
end

namespace :org do
  desc "Interactively Create a new PandaPal::Organization"
  task :new, [:name] => :environment do |t, args|
    org = PandaPal::Organization.new(name: args[:name])
    PandaPal::Organization.interactive_create!(org)
  rescue PandaPal::OrganizationConcerns::OrganizationBuilder::InteractiveSessionError => ex
    ex.print
  end

  desc "Interactively Update a PandaPal::Organization"
  task :edit, [:name] => :environment do |t, args|
    org = PandaPal::Organization.find_by!(name: args[:name])
    org.interactive_update!
  rescue PandaPal::OrganizationConcerns::OrganizationBuilder::InteractiveSessionError => ex
    ex.print
  end

  desc "Install/update the LTI in Canvas for the given Organization"
  task :install, [:name, :host] => :environment do |t, args|
    org = PandaPal::Organization.find_by!(name: args[:name])
    org.interactive_install!(host: args[:host], exists: :update)
  end

  desc "Reinstall the LTI in Canvas for the given Organization"
  task :reinstall, [:name, :host] => :environment do |t, args|
    org = PandaPal::Organization.find_by!(name: args[:name])
    org.interactive_install!(host: args[:host], exists: :replace)
  end

  if Rails.env.development?
    desc "Creates a new PandaPal::Organization for development"
    task dev: :environment do
      PandaPal::Organization.development_create!
    end
  end
end

end