### codetube
    Copyright (C) 2011 payload payload@lavabit.com
    Copyright (C) 2011 dodo dodo.the.last@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
###


views =
    projects:
        all:
            map: (doc) ->
                if doc.type is 'project'
                    emit(null, doc)
    activity:
        all:
            map: (doc) ->
                if doc.type is 'activity'
                    emit doc.date, doc
        'by-user-date':
            map: (doc) ->
                if doc.type == 'activity' and doc.user
                    emit [doc.user.id, doc.date], doc
                if doc.type == 'user'
                    emit [doc.id, ''], doc
                if doc.type == 'project'
                    emit [doc.owner.id, '', doc.id], doc
        'by-project-date':
            map: (doc) ->
                if doc.type == 'activity' and doc.project
                    emit [doc.project.id, doc.project.owner.id, doc.date], doc
                if doc.type == 'user'
                    for projectid in doc.projects
                        emit [projectid, doc.id, ''], doc
                if doc.type == 'project'
                    emit [doc.id, doc.owner.id, ''], doc

    user:
        all:
            map: (doc) ->
                if doc.type is 'user'
                    emit(null, doc)
        data:
            map: (doc) ->
                if doc.type is 'user'
                    ids = [doc._id]
                        #.concat doc.watching
                        #.concat doc.watchers
                    emit([id, ''], doc) for id in ids
                if doc.type is 'project'
                    ids = [doc.owner._id]
                        #.concat doc.contributors
                        #.concat doc.watchers
                    emit([id, ''], doc) for id in ids
                if doc.type is 'activity'
                    users = doc.watchers
                    emit([u._id, doc.date], doc) for u in users
        projects:
            # startkey: ["#{user.id}"], endkey: ["#{user.id}",{}]
            # results in many copies of user and all of his projects
            # key: ["#{user.id}", "#{project.id}"]
            # results in one user and the project
            map: (doc) ->
                if doc.type == 'project'
                    emit([doc.owner.id, doc.id], doc)
                else if doc.type == 'user'
                    emit([doc.id, projectid], doc) for projectid in doc.projects
                    emit([doc.id, null], doc) unless doc.projects.length

        commit_user_lookup:
            # lookup for user name
            map: (doc) ->
                if doc.type is 'user'
                    if doc.emails
                        for email, vars of doc.emails
                            # FIXME add validation and uncomment
                            #if not vars.validated
                            #    continue
                            emit(email, ["email", doc._id])
                            if vars.hash
                                emit(vars.hash, ["email", doc._id])
                    if doc.name
                        emit(doc.name, ["name", doc._id])
                    emit(doc.id, ["login", doc._id])
            reduce: (keys, values) ->
                log(keys)
                best_match = null
                for [type, value] in values
                    if type == 'email'
                        return _id: value
                    else if type == 'login'
                        best_match = ['login', value]
                    else if type != 'name'
                        best_match = ['name', value]
                return _id: best_match[1]




updates =
    list:
        concat: (doc, req) ->
            res = {}
            for key, value of req.query
                doc[key] = res[key] = doc[key].concat(value)
            return [doc, JSON.stringify(res)]

# exports

module.exports = {views, updates}

