# == Schema Information
#
# Table name: organisations
#
#  id                :integer          not null, primary key
#  organisation_name :string(255)
#  created_at        :datetime
#  updated_at        :datetime
#  no_of_aps         :integer          default(200)
#  sms_config        :text
#

class Organisation < ActiveRecord::Base
  include PublicActivity::Model

  MODEL_NAME_MAP = {"RouterInventory" => "Device", "LocationNetwork" => "Network", "NetworkSsid" => "Network SSID", "VLan" => "VLAN"}

  tracked owner: ->(controller, model) { controller && controller.tracked_current_user if controller},params:
  { :attributes => proc {|controller, model_instance| { "organisation(#{model_instance.organisation_name})" => model_instance.changes}}},organisation_id: ->(controller, model) { controller && controller.tracked_current_user.organisation_id if controller && controller.tracked_current_user}

  tracked assumed_by: proc {|controller, model| controller.user_assumed_by if controller}
  
  serialize :organisation_settings, Hash
  serialize :sms_config, Hash
  has_many :users
  # serialize :sms_config, Hash
  validate :check_domain_and_live_domain
  has_attached_file :organisation_logo, size: { in: 0..5.megabytes }, styles: { site: "350x250", map: "250x250", thumb: "141x91!" }
  validates_attachment_content_type :organisation_logo, content_type: /\Aimage\/.*\z/, :message => "Oops!, organisation logo is invalid, Please Choose the correct format"
  validates_attachment_file_name :organisation_logo, matches: [/png\z/, /jpe?g\z/, /jpg\z/, /gif\z/]
  has_many :users, :dependent => :destroy
  accepts_nested_attributes_for :users,                                 
                                #:reject_if => lambda { |attrs| attrs.all? { |key, value| value.blank? } },
                                :allow_destroy => true
  do_not_validate_attachment_file_type :organisation_logo
  has_many :location_networks
  has_many :router_inventories

  has_many :radius_configurations
  has_many :radiuses, class_name: "Radius"
  has_many :radius_accountings
  has_many :radio_profiles
  has_many :init_templates, dependent: :destroy

  has_many :acl_groups
  has_many :switch_configurations
  # has_many :static_routes
  has_many :portforwarding_groups
  has_many :snmp_configs
  has_many :sqms
  has_many :v_lans
  has_many :upgrades
  has_many :upgrade_datas
  has_many :commands
  has_many :alerts
  has_one :air_quality_config
  has_many :notification_groups,dependent: :destroy
  has_many :gateway_configs, dependent: :destroy
  has_many :sim_managements, dependent: :destroy
  has_many :purchase_orders, dependent: :destroy
  has_many :payment_invoices, dependent: :destroy
  has_many :sim_payment, dependent: :destroy
  has_one :roaming, dependent: :destroy
  has_many :integration_types, dependent: :destroy

  has_one :sms_config, -> { where is_enabled: true, gateway_type: '1' }, class_name: "GatewayConfig"
  has_one :email_config, -> { where is_enabled: true, gateway_type: '2' }, class_name: "GatewayConfig"
  
  def check_domain_and_live_domain
    if self.organisation_domain.present? && self.live_domain.present?
      errors.add(:live_domain, "can't be the same as domain") if self.organisation_domain == self.live_domain
    end
  end

end
