const common = require('./../../utils/common.js');
logger = require('../../../logger');
const cacheApi = require('./../../utils/semusi.cache.js');
let resultArray = [];
let counter     = 0;

function updateUsersSession(counter){

    if(resultArray[counter]){
        let updateSessionUserObj = resultArray[counter].updateSessionUserObj;
        let id = resultArray[counter].id;
        common.db.collection('timely_users_session').update({'_id': id}, {'$inc': updateSessionUserObj}, {'upsert': true},function(err, response){
            if(err){
                logger.error(`error=> ${err}`);   
            }
            else{
                 
            }
            logger.info(`counter =>  ${counter}`); 
            counter++;
            updateUsersSession(counter);
        });
    }
    else{
        logger.info("done!");
        process.exit(1);
    }
}

let elseCounter = 0;
function getUsersSession(appId, appName){
    logger.info(`app_users =>  ${appId}`);
    common.db.collection('app_users'+appId).find({"active":true},{'did':1,'p':1, 'sc':1, 'tsd':1, 'ls':1}).toArray(function(error, result){
        if(!error){
            if(result){
                logger.info(`result =>  ${result.length}`);
                resultArray = [];
                result.forEach(function(value, index){
                    //if(value.tsd<1000000 && value.tsd>0 ){

                        if(value.p && value.sc && value.tsd && value.ls){
                            let platform = value.p.toLowerCase();
                            let epoch = value.ls*1000;
                            let id = common.timelyEventId(appId, epoch);
                            let updateSessionUserObj = {};
                            common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "sc."+common.getActiveUserKey(value.sc));
                            common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "totalsc", parseInt(value.sc));
                            common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "asl."+common.getAverageSessionUserKey(value.tsd));
                            // if(value.tsd<10000 && value.tsd>0 ){
                             
                                common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "totalasl",parseInt(value.tsd));
                            //}

                            resultArray.push({updateSessionUserObj: updateSessionUserObj, id: id});

                        }
                        else{
                            if(value.p && value.ls){
                                let platform = value.p.toLowerCase();
                                let epoch = value.ls*1000;
                                let id = common.timelyEventId(appId, epoch);
                                let updateSessionUserObj = {};
                                common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "sc."+common.getActiveUserKey(1));
                                common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "totalsc",1);
                                common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "asl."+common.getAverageSessionUserKey(0));
                                common.fillEventObjectWithEpoch(epoch, updateSessionUserObj, platform+"."+appName, "totalasl",30);
                                resultArray.push({updateSessionUserObj: updateSessionUserObj, id: id})
                            }
                        }
                    // }
                    // else{
                     
                    // }
                });
                logger.info(`resultArray =>  ${resultArray.length}`);
                if(resultArray.length > 0){
                    counter     = 0;
                    //cacheApi.setKey("app_id"+appId,JSON.stringify({key:appId, name:appName}));
                    updateUsersSession(counter);
                }
            }
        }
    });
}

getUsersSession('5fa41afe6fc1e678a8344311', 'SBI Cards - Prod');
