var _ = require('underscore');
var app = require('../../server/server');
var dsConfig = require('../../server/datasources.json');

//12 hours in milliseconds
// var EMAILINTERVAL = 43200000;

//10 seconds for testing
var EMAILINTERVAL = 10000;

module.exports = function(UsedCode) {

    // this is the logic for taking in a request, removing an access code,
    // and sending the code to both the client and the used code table 

    UsedCode.process= function(requestObject, cb){

        var yourEmailAddress = dsConfig.emailDs.transports[0].auth.user;

        var response = requestObject.instance;
        //grab the object representing the SQL data
        var availableCodes = app.datasources.mydb.models.availableCodes;
        var contacts = app.datasources.mydb.models.contact;
        
        var prodId = requestObject.instance.productId;
        //search the SQL data for the correct model using the productId
        //maybe move thise to the before save function below?? that way we can avoid 
        //writing to the db if there's no codes
        availableCodes.findOne({where:{productId: prodId}}, function(err, row){
            if(err){
                console.log(err);
            }
            if(row){
                //add our new code to our response
                response.code = row.code;
                //add the date
                var date = new Date().toLocaleDateString();
                response.date = date;
                //delete the row
                availableCodes.destroyById(row.id, function(err){
                    //send the callback
                    cb(null, response);
                });

                
                availableCodes.count({productId: prodId}, function(err, count){
                    if(err){ 
                        console.log(err);
                    }
                    else if(count < 5){
                        console.log('productId ' + prodId + ' has ' + count + ' codes left');
                        contacts.findOne({where:{productId: prodId}}, function(err, contact){

                            var now = Date.now();

                            if(err){
                                console.log(err);
                            } 
                            else if(now - contact.lastEmailed > EMAILINTERVAL) {
                                var updated = contact;
                                updated.lastEmailed = now;

                                // app.models.Email.send({
                                //     to: 'gbuhler@wiley.com',
                                //     cc: 'chrcollier@wiley.com;gbuhler@wiley.com',
                                //     from: yourEmailAddress,
                                //     subject: 'The email subject',
                                //     text: 'The following code was just generated in Dispense: \n\n' + response.code + '\n\n There are ' + count + ' codes left for this product',
                                //     //html: '<strong>HTML</strong> tags are converted'
                                // }, function(err) {
                                //     if (err) throw err;
                                //     console.log('> email sent successfully');
                                // });

                                contacts.upsert(updated, function(err, instance){
                                    console.log('updated', instance);
                                })
                            }
                            else{
                                console.log('Contact has already been emailed within 12 hours');
                            }
                        })
                    }
                });
            }
            else {
                cb(true, null);
            }
        });
    };

    // commented for testing usedCode searching -- uncomment when done
    //
    // UsedCode.observe('before save',function(modelInstance, next) {

    //     // console.log('before save', modelInstance);

    //     UsedCode.process(modelInstance, function(err, res){
    //         modelInstance = res;
    //         next();
    //     });

    // });

   

    //register our "process" function as a remote method
    //available via our web api
    UsedCode.remoteMethod(
        'process',
        {
          accepts: {arg: 'requestObject', type: 'Object'},
          returns: {arg: 'receivedCode', type: 'Object'}
        }
    );

};

@@ -3902,19 +3902,16 @@
   //%0A   
- //
  UsedCod
@@ -3966,27 +3966,24 @@
 next) %7B%0A%0A   
- //
      // cons
@@ -4021,27 +4021,24 @@
 tance);%0A%0A   
- //
      UsedCod
@@ -4086,26 +4086,24 @@
 s)%7B%0A    
-//
         
  modelIn
@@ -4094,17 +4094,16 @@
         
- 
 modelIns
@@ -4114,27 +4114,24 @@
 e = res;%0A   
- //
          nex
@@ -4138,19 +4138,16 @@
 t();%0A   
- //
      %7D);
@@ -4151,19 +4151,16 @@
 %7D);%0A%0A   
- //
  %7D);%0A%0A  

fix silly thing