# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require 'rubygems'
require 'filestack'

require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

namespace :cache do
  task :clear => :environment do
    Rails.cache.clear
  end
end

task :announce_webinar => :environment do
  Webinar.all_times(type: "live").each do |webinar|
    if (webinar[:time] > Time.current) && (webinar[:time] <= (Time.current + 1.hour))
      Tea::Client.new("https://tea.teachable.cloud").emit_message({
        slack: [
          {
            channels: ["C02P1RMS1"],
            message: "Starting Soon . . ."
          },
          {
            channels: ["C02P1RMS1"],
            message: ">>> \n * #{webinar[:title]}* \n :mantelpiece_clock: #{webinar[:time_parsed]}"
          },
          {
            channels: ["C02P1RMS1"],
            message: "*Registrants*: #{webinar[:registrations]}"
          },
          {
            channels: ["C02P1RMS1"],
            message: "Admin chat: https://www.teachable.com/admin/webinars/#{webinar[:id]}/chat"
          },
          {
            channels: ["C02P1RMS1"],
            message: "Play page: https://www.teachable.com/webinars/#{webinar[:url]}/play"
          },
          {
            channels: ["C02P1RMS1"],
            message: "Replay page: https://www.teachable.com/webinars/#{webinar[:url]}/replay"
          }
        ]
      })
    end
  end
end

desc "Turns off maintenace partial and stop redirects for create account, login and forgot password pages"
task disable_maintenance_partial: :environment do
  ['/login', '/create-account', '/forgot-password', '/support-login'].each do |url|
    redirect = Redirect.find_by(url: url)
    Page.find_by(url: url).update(active: true)
    redirect.update(active: false)
  end
  Page.find_by(url: '/maintenance').update(active: false)
  Rails.cache.clear("views/##{ENV['CACHE_HOST_WITH_PORT']}/maintenance")
end

desc "Turns on maintenace partial and redirects for create account, login and forgot password pages"
task enable_maintenance_partial: :environment do
  Page.find_by(url: '/maintenance').update(active: true)
  ['/login', '/create-account', '/forgot-password', '/support-login'].each do |url|
    redirect = Redirect.find_by(url: url)
    redirect ? redirect.update(active: true) : Redirect.create!(url: url, redirect: '/maintenance', status_code: '302')
    Page.find_by(url: url).update(active: false)
    Rails.cache.clear("views/##{ENV['CACHE_HOST_WITH_PORT']}#{url}")
  end
end

task :release => ["db:migrate"]

task :update_iterable_webinars_metadata, [:webinar_id] => [:environment] do |t, args|
  Iterable.update_webinar(args[:webinar_id])
end

task :set_up_local_aaa do
end

desc "Syncs all existing user accounts to AAA with a real password"
task create_user_accounts_in_aaa: :environment do
  password = ENV["DEFAULT_AAA_PASSWORD"].presence || "password"
  encrypted_password = BCrypt::Password.create(password).to_s
  User.all.each do |user|
    puts "Creating user #{user.email}..."
    res = AAA.client.sync_membership(
      site_id:             "cms",
      member_id:           user.id.to_s,
      email:               user.email,
      name:                user.name,
      encrypted_password:  encrypted_password,
    )
    if res.status.to_i != 200
      puts res.body.to_s
      exit 1
    end
  end
end
