class Employee < ApplicationRecord
  has_secure_password
  has_many :commissioners
  has_many :shit_sales
  has_one  :current_shift, -> { where.not(shift_started_at: nil).where(shift_closed_at: nil) },class_name: "ShiftSale"
  has_one  :cashier_terminal, through: :current_shift
  belongs_to :order_queue_station

  validates_presence_of :name, :role
  validates_presence_of :password, :on => [:create]
  validates :emp_id, numericality: true, length: {in: 1..4}, allow_blank: true
  validates :emp_id, uniqueness: { scope: :shop_code }, if: -> { Employee.columns.map { |column| column.name }.include?('shop_code') }
  validates :emp_id, uniqueness: true, if: -> { !Employee.columns.map { |column| column.name }.include?('shop_code') }
  validates :password, numericality: true, length: {in: 3..9}, allow_blank: true

  before_create :generate_app_id, :generate_app_token,
    if: Proc.new { self.role == "application" }
  before_update :generate_app_id, :generate_app_token,
    if: Proc.new { self.role == "application" && self.role_was != "application" }

  # Employee Image Uploader
  mount_uploader :image_path, EmployeeImageUploader

  def self.all_emp_except_waiter
     Employee.where('role!=?','waiter')
  end

  def self.collection
    Employee.select("id, name").map { |e| [e.name, e.id] }
  end

  def self.login(emp_id, password)
    user = Employee.find_by_emp_id(emp_id)
    expiry_time = login_expiry_time
    if (user)
      #user.authenticate(password)
      if (user.authenticate(password))
        user.generate_token
        user.session_expiry = Time.now + expiry_time.minutes
        user.session_last_login = Time.now
        user.save
        return user
      end
    end
    return nil
  end

  def self.authenticate_by_token(session_token)
    if (session_token)
      user = Employee.find_by_token_session(session_token)
      expiry_time = login_expiry_time

      if user && user.session_expiry && user.session_expiry.utc > Time.current.utc
        #Extend the login time each time authenticatation take place
        user.session_expiry = user.session_expiry.utc + expiry_time.minutes
        user.save
        return true
      else
        return false
      end
    end

    return false
  end

  def self.logout(session_token)
    if (session_token)
      user = Employee.find_by_token_session(session_token)

      if user
        user.token_session = nil
        user.session_expiry = nil
        user.save
      end
    end
  end

  def generate_token
      update_column :token_session, SecureRandom.hex(10)
    rescue ActiveRecord::RecordNotUnique
      retry
  end

  def self.login_expiry_time
    expiry_time = 30
    login_expiry = Lookup.collection_of('expiry_time')
    if !login_expiry.empty?
      login_expiry.each do |exp_time|
        if exp_time[0].downcase == "login"
          expiry_time = exp_time[1].to_i
        end
      end
    end

    return expiry_time
  end

  def generate_app_id
    self.app_id = SecureRandom.hex(8)
  rescue ActiveRecord::RecordNotUnique
    retry
  end

  def generate_app_token
    self.app_token = SecureRandom.hex(10)
  rescue ActiveRecord::RecordNotUnique
    retry
  end
end
