<commit_before># frozen_string_literal: true

# User model, uses devise to manage registrations. Each user has a team reference which is
# set to nil until they are added to a team.
class User < ApplicationRecord
  include ActionView::Helpers::UserHelper

  has_paper_trail only: %i[
    email
    team_id
    full_name
    affiliation
    year_in_school
    age
    state
    compete_for_prizes
    admin
    current_sign_in_ip
  ]

  mount_uploader :resume, ResumeUploader
  mount_uploader :transcript, TranscriptUploader

  belongs_to :team, counter_cache: true, optional: true
  has_many :feed_items, dependent: :destroy
  has_many :user_invites, dependent: :destroy
  has_many :user_requests, dependent: :destroy
  has_many :submitted_flags, dependent: :destroy

  geocoded_by :current_sign_in_ip
  after_validation :geocode, unless: :geocoded?

  scope :interested_in_employment, -> { where(interested_in_employment: true) }

  reverse_geocoded_by :latitude, :longitude do |obj, results|
    if (geo = results.first)
      obj.country = case geo.country
                    when 'USA'
                      'United States'
                    when 'RSA'
                      'South Africa'
                    when 'ROC'
                      'Taiwan'
                    when 'PRC'
                      'China'
                    else
                      geo.country
                    end
    end
  end
  after_validation :reverse_geocode, if: ->(obj) { obj.latitude_changed? || obj.longitude_changed? }

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable,
         :rememberable, :trackable, :confirmable, :secure_validatable

  # These are things we require user to have but do not require of admins.
  with_options unless: :admin? do
    before_save :clear_compete_for_prizes
    before_destroy :leave_team_before_delete
    after_create :link_to_invitations
    after_update :update_team
    validates :email, uniqueness: true
    validates :full_name, :affiliation, presence: true, length: { maximum: 255 }, obscenity: true
    validates :state, presence: true
    validates :age, numericality: { greater_than_or_equal_to: 0, less_than: 200 }, allow_blank: true
    validates :year_in_school, inclusion: { in: [0, 9, 10, 11, 12, 13, 14, 15, 16] }, presence: true
  end

  def self.user_editable_keys
    # First line required fields, second line optional
    # Really just broken out into 2 lines to appease rubocop
    %i[
      full_name affiliation year_in_school state
      compete_for_prizes interested_in_employment gender age area_of_study resume transcript
    ]
  end

  # Returns whether a user is currently on a team or not.<commit_msg>Emails should be both present and unique<commit_after># frozen_string_literal: true

# User model, uses devise to manage registrations. Each user has a team reference which is
# set to nil until they are added to a team.
class User < ApplicationRecord
  include ActionView::Helpers::UserHelper

  has_paper_trail only: %i[
    email
    team_id
    full_name
    affiliation
    year_in_school
    age
    state
    compete_for_prizes
    admin
    current_sign_in_ip
  ]

  mount_uploader :resume, ResumeUploader
  mount_uploader :transcript, TranscriptUploader

  belongs_to :team, counter_cache: true, optional: true
  has_many :feed_items, dependent: :destroy
  has_many :user_invites, dependent: :destroy
  has_many :user_requests, dependent: :destroy
  has_many :submitted_flags, dependent: :destroy

  geocoded_by :current_sign_in_ip
  after_validation :geocode, unless: :geocoded?

  scope :interested_in_employment, -> { where(interested_in_employment: true) }

  reverse_geocoded_by :latitude, :longitude do |obj, results|
    if (geo = results.first)
      obj.country = case geo.country
                    when 'USA'
                      'United States'
                    when 'RSA'
                      'South Africa'
                    when 'ROC'
                      'Taiwan'
                    when 'PRC'
                      'China'
                    else
                      geo.country
                    end
    end
  end
  after_validation :reverse_geocode, if: ->(obj) { obj.latitude_changed? || obj.longitude_changed? }

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable,
         :rememberable, :trackable, :confirmable, :secure_validatable

  # These are things we require user to have but do not require of admins.
  with_options unless: :admin? do
    before_save :clear_compete_for_prizes
    before_destroy :leave_team_before_delete
    after_create :link_to_invitations
    after_update :update_team
    validates :email, uniqueness: true, presence: true
    validates :full_name, :affiliation, presence: true, length: { maximum: 255 }, obscenity: true
    validates :state, presence: true
    validates :age, numericality: { greater_than_or_equal_to: 0, less_than: 200 }, allow_blank: true
    validates :year_in_school, inclusion: { in: [0, 9, 10, 11, 12, 13, 14, 15, 16] }, presence: true
  end

  def self.user_editable_keys
    # First line required fields, second line optional
    # Really just broken out into 2 lines to appease rubocop
    %i[
      full_name affiliation year_in_school state
      compete_for_prizes interested_in_employment gender age area_of_study resume transcript
    ]
  end

  # Returns whether a user is currently on a team or not.
{'fraction_non_alphanumeric': 0.0684811237928007, 'fraction_numerical': 0.008428446005267778, 'mean_word_length': 2.969337979094077, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4779090', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1642, 'n_words': 515}