#
# The release_candidates collection contains the configuration for a set of docker builds
# that constitute a release candidate.
#
# @property createdAt [Number] when this release candidate was built
# @property projectId [uuid] the mapping of service name to branch name to build for this release
# @property serviceBranchMap [Object] the mapping of service name to branch name to build for this release candidate
# @property updatedAt [Number] when this release candidate was updated
# @property userId [Number] the user who created the release candidate
module.exports = (Model) ->
  ReleaseCandidate = Model.collection 'ReleaseCandidate', 'release_candidates'

  # 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
  ReleaseCandidate.build = (options) ->
    throw new Error 'options missing' unless options

    release =
      # Do we need this? Or should we put a releaseId on each build and query that way?
      createdAt: Date.now()
      projectId: options.projectId
      serviceBranchMap: options.serviceBranchMap || {all: 'master'}
      updatedAt: Date.now()
      userId: options.userId || @model.get('_session.userId')

    return release

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