require 'messages/list_message'

module VCAP::CloudController
  class AppsListMessage < ListMessage
    ALLOWED_KEYS = [:names, :guids, :organization_guids, :space_guids, :page, :per_page, :order_by, :order_direction].freeze

    attr_accessor(*ALLOWED_KEYS)

    def initialize(params={})
      super(params.symbolize_keys)
    end

    validates_with NoAdditionalParamsValidator

    validates :names, array: true, allow_nil: true
    validates :guids, array: true, allow_nil: true
    validates :organization_guids, array: true, allow_nil: true
    validates :space_guids, array: true, allow_nil: true

    def to_param_hash
      super(exclude: [:page, :per_page, :order_by])
    end

    def self.from_params(params)
      opts = params.dup
      %w(names guids organization_guids space_guids).each do |attribute|
        to_array! opts, attribute
      end
      new(opts.symbolize_keys)
    end

    def valid_order_by_values
      super << :name
    end

    private

    def allowed_keys
      ALLOWED_KEYS
    end
  end
end
