module JamRuby
  class EmailProfileReminder
    @@log = Logging.logger[EmailProfileReminder]

    def self.send_reminders
      begin
        #If the user has not updated their profile 1 day after signup, then send reminder email1
        reminder1_users.find_each do |user|
          UserMailer.profile_complete_reminder1(user).deliver_now
          User.where(id: user.id).update_all(profile_complete_reminder1_sent_at: Time.now)
        end

        #If the user has not updated their profile 3 days after signup, then send reminder email2
        reminder2_users.find_each do |user|
          UserMailer.profile_complete_reminder2(user).deliver_now
          User.where(id: user.id).update_all(profile_complete_reminder2_sent_at: Time.now)
        end

        #If the user has not updated their profile 5 days after signup, then send reminder email3
        reminder3_users.find_each do |user|
          UserMailer.profile_complete_reminder3(user).deliver_now
          User.where(id: user.id).update_all(profile_complete_reminder3_sent_at: Time.now)
        end
      rescue Exception => e
        @@log.error("unable to send profile reminder email=#{e}")
        puts "unable to send profile reminder email=#{e}"
      end
    end

    def self.prospect_users
      User.where("users.profile_completed_at IS NULL AND users.subscribe_email = ?", true)
    end

    def self.reminder1_users
      EmailProfileReminder.prospect_users.where("users.created_at < ? AND users.profile_complete_reminder1_sent_at IS NULL", 1.day.ago)
    end

    def self.reminder2_users
      EmailProfileReminder.prospect_users.where("users.created_at < ? AND users.profile_complete_reminder1_sent_at IS NOT NULL AND users.profile_complete_reminder2_sent_at IS NULL", 3.days.ago)
    end

    def self.reminder3_users
      EmailProfileReminder.prospect_users.where("users.created_at < ? AND users.profile_complete_reminder2_sent_at IS NOT NULL AND users.profile_complete_reminder3_sent_at IS NULL", 5.days.ago)
    end

   
  end
end