const db = require("./mongo");
const sendEmail = require("./send-email");

async function afterStopVms( provision, auto ) {
    let timeRunning = db.utils.getNewTimeRunning(provision);
    const dateNow = new Date();
    let patch = {
        "statusVms": "Stopped", 
        "timeRunning": timeRunning, 
        "stoppedFrom": dateNow, 
        "pendingNextAction": undefined
    };    
    
    if ( auto && provision._scenarioDoc ) { //From CLI (auto stop)
        let msg = "";
        // Actual onschedule reset
        if ( provision.schedule && !provision.schedule.is24x7 ) {
            patch["startDateOnSchedule"] = dateNow;
            patch["endDateOnSchedule"] = dateNow;
            msg = "Schedule time has been reset.";
        } else {
            msg = "24x7 vms auto-stopped due to limitted running time reached.";
        }

        msg += `New totalTimeRunning: ${timeRunning} mins`;

        await db.provision.update(provision._id.toString(), patch);
        await sendEmail.sendVMsStopped(provision, provision._scenarioDoc);
        db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.stop-auto', message: msg });
    
    } else { //On Demand stop
        let msg = "";
        if ( provision.schedule && !provision.schedule.is24x7 ) {
            patch["endDateOnSchedule"] = dateNow;

            //This is temporary, only to make sure there is some initial value soon
            if ( !provision["startDateOnSchedule"] ) {
                patch["startDateOnSchedule"] = dateNow;
                msg = "startDateOS: " + dateNow.toISOString();
            } else {
                msg = "startDateOS: " + new Date(provision.startDateOnSchedule).toISOString();
            }
            msg += (" - endDateOS: " + dateNow.toISOString());
            
        }

        msg += ` - New totalTimeRunning: ${timeRunning} mins`;

        await db.provision.update(provision._id.toString(), patch);
        
        db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.stop-ondemand', message: msg });
    }
}

async function afterStartVms( provision ) {
    const dateNow = new Date();
    let countExtend = db.utils.getNewCountExtend(provision);
    var patch = {
        "statusVms": "Running", 
        "runningFrom": dateNow, 
        "countExtend": countExtend, 
        "pendingNextAction": undefined
    };

    // Actual onschedule reset
    let msg = "";
    if ( provision.schedule && !provision.schedule.is24x7 ) {
        msg = "Schedule time has been reset.";
        patch["startDateOnSchedule"] = dateNow;
        patch["endDateOnSchedule"] = dateNow;
    } else {
        msg = `24x7, New count extend: ${countExtend}`;
    }

    msg += `TotalTimeRunning so far: ${provision.timeRunning} mins`;

    await db.provision.update(provision._id.toString(), patch);

    db.event.add({ user: provision.user._id, provision: provision._id, type: 'vms.start-ondemand', message: msg });

}

module.exports.afterStopVms = afterStopVms;
module.exports.afterStartVms = afterStartVms;