import _ from 'lodash'
import path from 'path'

export default function profile(opts) {

  opts = Object.assign({
    query: 'profile',
    basedir: './models'
  }, opts)

  let {collection} = opts

  if (!collection) {
    throw new Error('Collection must be defined')
  }

  /**
   * @scope {Resource}
   * @param {Object} req
   * @param {Object} res
   * @returns {*}
   */
  return function(req, res) {
    let file
    let schema

    if (req.query[opts.query] && req.query[opts.query] !== 'default') {
      // TODO Load profiles during start time
      file =
        path.join(
          path.dirname(require.main.filename),
          opts.basedir,
          collection.modelName.toLowerCase() +
          '.profile.' +
          req.query[opts.query] +
          '.json'
        )
      try {
        schema = require(file)
      } catch (err) {
        return console.error(err)
      }
      res.body = _.pick(res.body, schema)
    }
  }

}

@@ -27,24 +27,93 @@
 ort 
-path from 'path'
+dbg from 'debug'%0Aimport path from 'path'%0A%0Aconst debug = dbg('netiam:plugins:profile')
 %0A%0Aex
@@ -930,28 +930,13 @@
     
-return console.error
+debug
 (err

add debug statement to profile plugin