module JamRuby
  class IcecastSourceChange < ActiveRecord::Base

    @@log = Logging.logger[IcecastSourceChange]

    CHANGE_TYPE_CLIENT = 'client'
    CHANGE_TYPE_MOUNT_UP_REQUEST = 'source_up_request'
    CHANGE_TYPE_MOUNT_DOWN_REQUEST = 'source_down_request'


    belongs_to :mount, class_name: "JamRuby::IcecastMount", inverse_of: :source_changes, foreign_key: 'icecast_mount_id'
    belongs_to :user, class_name: "JamRuby::User"

    validates :source_direction, inclusion: {:in => [true, false]}
    validates :success, inclusion: {:in => [true, false]}
    validates :reason,  length: {minimum: 0, maximum:255}, allow_nil: true
    validates :detail,  length: {minimum: 0, maximum:10000}, allow_nil: true
    validates :user,  presence:true, :if => :is_client_change?
    validates :client_id,  presence: true, :if => :is_client_change?
    validates :mount,  presence:true
    validates :change_type, inclusion: {:in => [CHANGE_TYPE_CLIENT, CHANGE_TYPE_MOUNT_DOWN_REQUEST, CHANGE_TYPE_MOUNT_UP_REQUEST]}

    def is_client_change?
      change_type == CHANGE_TYPE_CLIENT
    end
  end
end
