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

    def self.send_survey
      begin
        # if signup survey email has not been sent to this user, then send it
        survey_users.find_each do |user|
          UserMailer.signup_survey(user).deliver_now
          User.where(id: user.id).update_all(signup_survey_sent_at: Time.now)
        end
      rescue Exception => e
        @@log.error("unable to send surevy email e=#{e}")
        puts "unable to send surevy email e=#{e}"
      end
    end

    def self.survey_users
      cutoff_date = Date.parse(Rails.application.config.signup_survey_cutoff_date) # Define a cutoff date for the survey/gear setup emails
      User.where("users.signup_survey_sent_at IS NULL AND users.created_at < ? AND users.created_at > ?", 1.days.ago, cutoff_date)
    end
  end
end