#
# The projects collection contains the configuration for a project.
# A project consists of a set of features available for release.
# A release candidate belongs to a project, and consists of a set of docker builds.
#
# @property createdAt [Number] when this project was built
# @property latestReleaseId [uuid] the id of the most recent release
# @property projectName [String] the name of the project
# @property serviceBranchMap [Object] the mapping of service name to branch name to build for this project
# @property updatedAt [Number] when this project was updated
# @property userId [Number] the user who created the project
module.exports = (Model) ->
  Project = Model.collection 'Project', 'projects'

  # Build a release document
  #
  # @param options [Object] object prodiving the @options below, the initial properties for the release.
  # @option serviceBranchMap [Object] the mapping of service name to branch name to build for this release
  # @option userId [uuid] the user creating the release candidate
  Project.build = (options) ->
    throw new Error 'options missing' unless options

    project =
      createdAt: Date.now()
      latestReleaseId: options.latestReleaseId
      projectName: options.projectName
      serviceBranchMap: options.serviceBranchMap || {all: 'master'}
      updatedAt: Date.now()
      userId: options.userId || @model.get('_session.userId')

    return release

  # Adds a new project to the collection
  # See Project.build for options definition
  #
  # @param options [Object] options for building the project
  Project.addNew = (options) ->
    project = @build options
    return @add project
