var request = require('superagent');
var secrets = require('../client_secrets'); // TODO shouldn't have to require this again, already in v1oauth
var serverBaseUri = secrets.web.server_base_uri; // TODO shouldn't have to look this up. Use v1oauth.
var DataStore = require('nedb');
var path = require('path');
var Q = require('q');
var debug = require('debug')('query-v1');


/*\
 * Set up data store
\*/

var users = new DataStore({
  filename: path.join(__dirname, '..', 'cache', 'users.nedb'),
  autoload: true,
  onload: function (err) {
    if (err) console.log('error loading user database', err);
  }
});

users.ensureIndex({fieldName: '_oid', unique: true}, function (err) {
  if (err) {
    debug('err indexing db', err);
  } else {
    debug('successfully added _oid index');
  }
});

/*
 * GET users listing.
 */

exports.list = function(req, res){
  var query = {
    from: 'Member',
    select: [
      'Name', 'Nickname'
    ]
  };
  var token = req.cookies.v1accessToken; // TODO shouldn't read off of the cookie, let v1oauth know that stuff.  
  request
    .get(serverBaseUri + '/query.v1') // TODO v1oauth should help with this url
    .set('Authorization', 'Bearer ' + token) // TODO v1oauth should help with setting this header
    .send(query)
    .end(function (queryRes) {
      if (queryRes.ok) {
        // TODO read flagged users and set their check-boxes
        res.render('users', { title: 'All Users', users: queryRes.body[0]});
      } else {
        res.send('failure :-(\n' + queryRes.text);
      }
    });
};

exports.postList = function (req, res) {
  // TODO unflag those flagged in the DB and not in this post.
  var upsertPromises = [];
  req.body.selectedUsers.forEach(function (userOid) {
    upsertPromises.push(Q.ninvoke(users, "update", {_oid: userOid}, {$set: {flagged: true, _oid: userOid}}, {upsert: true}));
  });
  Q.allSettled(upsertPromises).then(function (states) {
    debug('upsertsDone %s', JSON.stringify(states, null, ' '));
    res.redirect('/users');
  });
};
@@ -609,60 +609,52 @@
 );%0A%0A
-users.ensureIndex(%7BfieldName: '_oid', unique: true%7D,
+function indexReporter(indexName) %7B%0A  return
  fun
@@ -667,16 +667,18 @@
 (err) %7B%0A
+  
   if (er
@@ -678,24 +678,26 @@
  if (err) %7B%0A
+  
     debug('e
@@ -703,20 +703,35 @@
 err 
-indexing db'
+adding index %25s', indexName
 , er
@@ -736,16 +736,18 @@
 err);%0A  
+ 
+ 
 %7D else %7B
@@ -743,24 +743,26 @@
    %7D else %7B%0A
+  
     debug('s
@@ -771,39 +771,225 @@
 cess
-fully added _oid index');%0A  %7D%0A%7D
+ adding index %25s', indexName);%0A    %7D%0A  %7D;%0A%7D%0A%0Ausers.ensureIndex(%7BfieldName: '_oid', unique: true%7D, indexReporter('_oid'));%0Ausers.ensureIndex(%7BfieldName: 'flagged', unique: false, sparse: true%7D, indexReporter('flagged')
 );%0A%0A

Add index for flagged users