var mongoose = require('mongoose');
// Define Model Schema
var taskSetSchema = mongoose.Schema({
    _id: String, // What MTurk refers to as a HITId
    // The lock let's us save a "placeholder" task set
    // One that is not yet completed, but it in-progress
    // This allows us to lock in-progress items.
    lock:Boolean,
    // Status to mimic the MTurk status
    // see http://mechanicalturk.typepad.com/blog/2011/04/overview-lifecycle-of-a-hit-.html
    status:{type:String},
    user: String, //Should be a hashed version of WorkerId
    hit_id:String, //My unique key for the HIT (Mongo HIT _id, not hitTypeID)
    facet_id:String, //Id of facet, if used (embedded doc in hit.facets)
    turk_hit_id: String, // Amazon's key for the HIT
    assignment_id:String,
    time:{
        start: Date, //AcceptTime
        submit: Date, //SubmitTime
        workTime: Number //WorkTimeInSeconds
    },
    feedback:{ 
        form: String,
        satisfaction: String,
        pay:String
    },
    payment: {type:Number, default: 0},
    // Total Bonus (for task, + per item bonuses)
    bonus: {type:Number, default: 0},
    tasks:[{
        //'type' is a reserved word, so when I want 'type'
        // to actually be in my schema, it needs to be defined
        // with { type: String }
        type: {type: String}, //Match hit.type e.g. 'Image tagging', 
        item:{
            type: { type: String }, //e.g. 'pin'; should reference model
            id:Number
        },
        timeSpent: Number,
        bonus: {type:Number, default: null},
        contribution:mongoose.Schema.Types.Mixed
    }]
});

// Define Class Methods
taskSetSchema.statics.countUserAll = function(user, callback) {
    return this.count({user:user, lock:{$ne:true}}).exec(callback);
};

taskSetSchema.statics.countUserHIT = function(user, hit_id, callback) {
    return this.count({user:user, hit_id:hit_id, lock:{$ne:true}}).exec(callback);
};

taskSetSchema.statics.clearLocks = function(user, callback) {
    // Remove tasksets with expired locks 
    var find_outdated_locks = {
        lock:true,
        $or:[
            // If the user already has a lock on something else on, unlock it
            {user:user},
            // If the start time is older than (Now-30m), it's a stale lock
            {'time.start':{$lte:new Date(new Date()-30*60*1000)}}
        ]
    };
    return this.remove(find_outdated_locks, callback);
};

// List of all items a user has completed
taskSetSchema.statics.userItemList = function(user, callback){
    return this.aggregate([
            {$match: {user:user, lock:{$ne:true}}},
            {$project:{"facet_id":1, "tasks.item.id":1}},
            {$unwind : "$tasks"},
            {$group: {_id:"$tasks.item.id"}}
    ]).exec(callback);
};

// Verify a bonus amount
taskSetSchema.statics.verifyBonus = function(_id, bonus, callback){
    this.findOne({_id:_id, bonus:bonus}, function(error, results) {
        if (error) { return callback(error); }
        if (!results || results.length === 0) {
            return callback("Didn't find assignment " + _id +
                "with a bonus of " + bonus);
        } else {
            // First test passed, no verify that the bonus *amount* was correct
            // based on HIT.payment.bonus.items
            // TODO
            return callback(null);
        }
    });
};

// Define Instance Methods

// Create Model and Export

var TaskSet = mongoose.connection.model('tasksets', taskSetSchema);

module.exports = TaskSet; 

@@ -624,32 +624,32 @@
     
-facet_id:String, //Id of
+// Information about the
  fac
@@ -661,16 +661,31 @@
 f used (
+corresponds to 
 embedded
@@ -691,16 +691,23 @@
 d doc in
+%0A    //
  hit.fac
@@ -709,17 +709,124 @@
 t.facets
-)
+%0A    facet: %7B%0A        _id: mongoose.Schema.Types.ObjectId,%0A        meta: mongoose.Schema.Types.Mixed,%0A    %7D,
 %0A    tur
@@ -2767,16 +2767,17 @@
 :%7B%22facet
+.
 _id%22:1, 
@@ -2857,16 +2857,42 @@
 p: %7B_id:
+%7Bfacet:%22$facet._id%22, item:
 %22$tasks.
@@ -2901,16 +2901,17 @@
 em.id%22%7D%7D
+%7D
 %0A    %5D).

Return user lists with facet