// FUNCTIONS
//  init()   : Exported: Uses the internal functions to initialise databases.
//  check()  : Checks data presence and age.
//  build()  : Builds/Rebuilds collections.

const log      = require('../utils/log.utils');          // Log Helper
const time     = require('../utils/timeConvert.utils');  // Time Helper
const corpus   = require('../services/corpus.services');
const dbAccess = require('../services/dbAccess.services');

async function init(){
    var status = await check('corpus');
    if (status == "not_ready") {
        try {
            build("corpus")
        } catch (err) {
            log.out("dbInitUtils.init: Error building corpus database")
            log.out(err)
        }
    }

    var status = await check('stations')
    if (status == "not_ready") {
        try {
            build("stations")
        } catch (err) {
            log.out("dbInitUtils.init: Error building stations database")
            log.out(err)
        }
    }
}

async function check(coll){
    log.out(`dbInitUtils.check: Checking collection '${coll}'`)
    try {
        var queryStr = {'type':'collection','target': coll};
        var res = await dbAccess.query('meta',queryStr);
        log.out(`dbInitUtils.check: Last update of ${coll}: ${time.unixLocal(res['0']['updated'])}`)
        var now = time.jsUnix(Date.now())
        var delta = now - res['0']['updated']
    } catch (err) {
        log.out(`dbInitUtils.check: Unable to find out data age.  Presume stale. Error Message:`)
        log.out(err)
        var delta = 12096000 // Extra zero to ensure data is updated.
    }

    var maxAge = 1209600 // 14 Days
    if (delta > maxAge) {
        log.out(`dbInitUtils.check: '${coll}' data older than max age ${maxAge} seconds.  Update pending`)
        return "not_ready"
    } else {
        log.out(`dbInitUtils.check: '${coll}' data newer than max age ${maxAge} seconds.  Update not required`)
        return "ready"
    }
}

async function build(db){  // `db` must be one of: `corpus`, `stations`, `all`.
    log.out("dbInitUtils.build: Building database structure")
    var corpusAll = await corpus.get();
    if (db === "corpus") {
        await dbAccess.dropCollection("corpus");
        dbAccess.putCorpus(corpusAll);

        log.out(`dbInitUtils.build: Updating corpus meta`)
        dbAccess.updateMeta("collection", "corpus", time.jsUnix(Date.now()));
    }
    if (db === "stations") {
        await dbAccess.dropCollection("stations");
        var corpusSubset = await corpus.subset(corpusAll);
        dbAccess.putStations(corpusSubset);

        log.out(`dbInitUtils.build: Updating stations meta`)
        dbAccess.updateMeta("collection", "stations", time.jsUnix(Date.now()));
    }
}

module.exports = {
    init
}