class User < ActiveRecord::Base
  has_many :memberships, dependent: :destroy
  has_many :groups, through: :memberships
  has_many :attendances, foreign_key: "attendee_id", dependent: :destroy
  has_many :events, through: :attendances
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save :downcase_email
  before_create :create_activation_digest
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
  validates :hours, presence: true,
                    numericality: { greater_than_or_equal_to: 0 }


  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(self.remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Activates an account.
  def activate
    update_columns(activated: true, activated_at: Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

  # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_columns(reset_digest:  User.digest(reset_token),
                   reset_sent_at: Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

  # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

  # Joins a group.
  def join(group)
    memberships.create(group: group)
  end

  # Quits a group.
  def quit(group)
    memberships.find_by(group: group).destroy!
  end

  # Attends an event.
  def attend(event)
    attendances.create(event: event)
  end

  # Leave an event.
  def leave(event)
    attendances.find_by(event: event).destroy!
  end

  # Returns true if the user is the admin of some group.
  def admin_of?(group)
    memberships.exists?(group_id: group.id, admin: true)
  end

  def membership_for(group)
    memberships.where(group_id: group.id).first
  end

  # Gives the attendance for an event.
  def attendance_for(event)
    attendances.where(event_id: event.id).first
  end

  def hours_for_group(group)
    group.has_member?(self) ? membership_for(group).hours : 0
  end

  def got_checked_for?(event)
    attendance = attendance_for(event)
    attendance && attendance.checked?
  end

  # Returns the last 5 events (that passed) that the user attended.
  def last_n_events(n)
    events.where(['date < ?', Date.today]).first(5)
  end

  private

    # Converts email to all lower-case.
    def downcase_email
      email.downcase!
    end

    # Creates and assigns the activation token and digest.
    def create_activation_digest
      self.activation_token = User.new_token
      self.activation_digest = User.digest(activation_token)
    end
end
