# == Schema Information
#
# Table name: alert_rules
#
#  id         :integer          not null, primary key
#  alert_on   :string(255)
#  condition  :string(255)
#  value      :string(255)
#  alert_id   :integer
#  created_at :datetime
#  updated_at :datetime
#  mac_range  :string(255)
#

class AlertRule < ActiveRecord::Base
  ALERT_ON = {"router" => ["Device", "Ethernet throughput", "SSID throughput", "Free memory", "Load average"],
              "client" => ["Client throughput", "Client", "Connected clients count", "Captive portal"],"network" => ["Network uplink"], "activity" => ["NetworkSsid activity"]}
  ALERT_CONDITION = {"Above" => "above", "Below" => "below", "Equal to" => "equal", "Associated" => "associated", "Disassociated" => "disassociated", "Up" => "up", "Down" => "down","Switch Over" => "uplink_switch_over", "Rebooted" => "reboot", "Loggedin" => "login", "Loggedout" => "logout","Upgrade" => "upgrade", "All events" => "all", "Create" => "create", "Update" => "update", "Destroy" => "delete"}

  belongs_to :alert
  include PublicActivity::Model

  tracked owner: ->(controller, model) { controller && controller.tracked_current_user },params: { :attributes => proc {|controller, model_instance| {"alert_rules(#{model_instance.alert_on})" => model_instance.changes}}},organisation_id: ->(controller, model) { controller && controller.tracked_current_user.organisation_id }, location_network_id: proc {|controller, model_instance| model_instance.alert.location_network_id}

  tracked assumed_by: proc {|controller, model| controller.user_assumed_by if controller}

  def check_rule_condition val
    case self.condition
    when "above"
      val > self.value.to_i
    when "below"
      val < self.value.to_i
    when "equal"
      val.to_s == self.value
    end
  end
end
