var extend = require('extend')

module.exports = function (options) {
  options = options || {}

  return function (server) {
    var model = require('./model')(server.db, options)
    var handler = require('./handler')(model, extend({ auth: server.auth }, options))
    var routes = require('./routes')(handler, options)

    model.name = 'embeds'
    model.handler = handler
    model.routes = routes

    model.serve = function (req, res) {
      return routes.match(req, res)
    }

    return model
  }
}