
var myArgs = process.argv.slice(2);

if ( myArgs.length < 2 ) {
    console.log("Missing args", myArgs);
    process.exit(0);
}

const db = require('qmi-cloud-common/mongo');
const sendEmail = require("qmi-cloud-common/send-email");
const moment = require('moment');
const fetch = require('node-fetch');


const IS_REAL = myArgs[1] !== 'test';
const WARNING_DAYS = 2;

//---

const API_KEY = process.env.API_KEY;
const SERVER_URL = "http://localhost:3000";

if ( !API_KEY ) {
    console.log("Missing env API_KEY");
    process.exit(0);
}

async function postDestroy(provision) {

    const userId = provision.user._id.toString();
    const provId = provision._id.toString();
    
    return fetch(`${SERVER_URL}/api/v1/users/${userId}/provisions/${provId}/destroy?apiKey=${API_KEY}`, {
        method: 'POST',
        headers:{
            'Content-Type': 'application/json'
        }
    })
	.then(res => res.json())
    .then(json => console.log(json));
}


function timeRunning(p) {
    let totalStopTime = Math.abs(new Date().getTime() - new Date(p.stoppedFrom).getTime());
    let duration = moment.duration(totalStopTime);
    p.duration = {
        hours: Math.floor(duration.asHours()),
        complete: Math.floor(duration.asDays()) +"d "+duration.hours()+"h "+duration.minutes()
    };
}

async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}

async function init(type) {
    var cb;
    var filter = {
        "isDestroyed":false, 
        "isDeleted": false, 
        "statusVms": "Stopped",
        "vmImage": {"$exists": true},
        "vmImage.vm1":  { "$exists": true }
    };
    if ( type === "warning" ) {
        filter.pendingNextAction = {$ne: "destroy"};
        cb = doSendEmailDestroyWarning;
    } else if ( type === "exec" ) {
        filter.pendingNextAction = "destroy";
        cb = doDestroy;
    } else {
        console.log("Invalid 'type'. Do nothing.");
        return;
    }

    let provisions = await db.provision.get(filter);

    await asyncForEach(provisions.results, async function(p)  {
        var typeSchedule = "24x7";
        if ( p.schedule && !p.schedule.is24x7 ) {
            typeSchedule = 'OnSchedule';
        }
        timeRunning(p);

        let stoppedPeriod = p._scenarioDoc.allowedInnactiveDays || 20;
        let stoppedPeriodExternal = Math.ceil(stoppedPeriod/2);

        var limit;
        if ( type === "warning" ) {
            limit = p.isExternalAccess? 24*(stoppedPeriodExternal-WARNING_DAYS) : 24*(stoppedPeriod-WARNING_DAYS);
        } else if ( type === "exec" ) {
            limit = p.isExternalAccess? (24*stoppedPeriodExternal) : (24*stoppedPeriod);
        }
        if ( !IS_REAL ) {
            console.log(`${p._id} (${typeSchedule}) - limit: ${limit} hs - actual duration: ${p.duration.hours} hs`);
        }
        if ( p.duration && p.duration.hours >= limit) {
            await cb(p, limit, typeSchedule);
        }
    });
}

const doSendEmailDestroyWarning = async function(p, limit, typeSchedule) {
    if ( p.pendingNextAction === 'destroy') {
        console.log(`Warning email Destroy already sent. Wait for pending action to complete.`);
    } else {
        let msg = `Send warning DESTROY email - ${p.user.displayName} (${p.user.upn}) about provision '${p._scenarioDoc.title}' (${p._id} - ${typeSchedule}) being 'Inactive' more than ${limit} hours, (exactly ${p.duration.complete})`;
        console.log(msg);  
        if ( IS_REAL ) {
            db.event.add({ user: p.user._id, provision: p._id, type: 'vms.warning-destroy' });
            await db.provision.update(p._id, {"pendingNextAction": "destroy"});
            await db.notification.add({ provision: p._id.toString(), type: 'warningDestroy', message: msg }); 
            await sendEmail.sendWillDestroyIn24(p, p._scenarioDoc, Math.floor(limit/24), WARNING_DAYS);   
            
        }        
    }  
};

const doDestroy = async function(p, limit, typeSchedule) {
    try {
        let msg = `Provision destroyed - ${p.user.displayName} (${p.user.upn}) about provision '${p._scenarioDoc.title}' (${p._id} - ${typeSchedule}) being 'Inactive' more than ${limit} hours (exactly ${p.duration.complete})`
        console.log(msg);
        if ( IS_REAL ) {    
            db.event.add({ user: p.user._id, provision: p._id, type: 'vms.exec-destroy' });     
            await postDestroy(p);
            await db.notification.add({ provision: p._id.toString(), type: 'destroy', message: msg });        
        }
    } catch (error) {
        console.log("doDestroy Error", error);
    }    
};

function check(type) {
    init(type).then(function(){
        db.mongoose.connection.close()
        process.exit(0);
        
    }).catch(function(e){
        db.mongoose.connection.close()
        console.log("Error", e);
        process.exit(0);
    });
}


// --------------------------------
switch (myArgs[0]) {
    case 'warning':
        check("warning");
        break;
    case 'exec':
        check("exec");
        break;
    default:
        console.log('Sorry, that is not something I know how to do.');
        process.exit(0);
}



