class Notification
  include Mongoid::Document
  #include PublicActivity::Model

  # tracked owner: ->(controller, model) { controller && controller.tracked_current_user },params:
  # { :attributes => proc {|controller, model_instance| {"notification(#{model_instance.alert_category})" => model_instance.changes}}},organisation_id: ->(controller, model) { controller && controller.tracked_current_user.organisation_id },location_network_id: proc {|controller, model_instance| Alert.find(model_instance.alert_id).location_network_id}

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

  #resourcify
  field :ap_mac_id, type: String
  field :alert_id, type: Integer
  field :location_network_id, type: Integer
  field :alert_category, type: String
  field :severity_color, type: String
  field :subject, type: String
  field :message, type: String
  field :alert_tags, type: Array
  field :is_read, type: Boolean, default: false
  field :is_sent, type: Boolean
  field :is_flagged, type: Boolean
  field :client_mac_id, type: String
  field :ssid, type: String
  field :extra_data, type: Hash
  field :created_at, type: Time

  index({"created_at" => 1}, { background: true })
  index({"is_flagged" => 1, "alert_id" => 1, "ap_mac_id" => 1}, { background: true })
  index({"is_read" => 1, "alert_id" => 1}, { background: true })
  index({ is_read: 1, ap_mac_id: 1},{background: true})

  def json_build
    alert = Alert.where(id: self.alert_id.to_i).first
    ln = LocationNetwork.where(id: self.location_network_id).last
    ri_name = ln.blank? ? nil : ln.router_inventories.where(mac_id: self.ap_mac_id).last.try(:name)
    {id: self.id, device_mac: self.ap_mac_id, alert_id: self.alert_id, alert_name: alert.try(:name), network_name: ln.try(:network_name), location_network_id: self.location_network_id, message: self.message, alert_category: (self.alert_category == "router" ? "device" : self.alert_category), alert_rules: (alert.alert_rules.first||{}).attributes.slice("alert_on", "condition", "value"),uplink_name: (self.extra_data||{})["uplink_name"], ssid: (self.extra_data||{})["ssid"], client_mac: (self.extra_data||{})["client_mac_id"], device_name: ri_name, timestamp: self.created_at.to_i}
  end
end
