'use strict';

var q = require("q");
var logger = require('../../utils/Logger').loggers.get('db');
const findService = require('./FindService');
/*
 * All functions in this service will retrieve data with no authenticated user or user keys...
 * */

function processUnactionedSystemActions(systemActionProcessingFunc) {
    return DBProvider.ruleDataService.processUnactionedSystemActions(systemActionProcessingFunc);
}

function getCurrentScheduledDocuments() {
    return DBProvider.ruleDataService.getCurrentScheduledDocuments();
}

function markSystemActionDone(systemAction, statusDescription) {
    return DBProvider.ruleDataService.markSystemActionDone(systemAction, statusDescription);
}

function getUser(userId) {
    var deferred = q.defer();


    var query = {"documentId": userId,
        "systemHeader.systemType": SharedConstants.SYSTEM_TYPE_ACCOUNT,
        "systemHeader.currentVersion": true
    };

    var params = {};
    params.query = query;
    DBProvider.dataFindService.findOne(params, null,
        function (queryErr, user) {
            if (queryErr) {
                var errorMsg = "Error getting user: " + queryErr;
                logger.error(errorMsg);
                deferred.reject(new Error(errorMsg));
            } else {
                if (user) {
                    logger.info("Found user with query: " + JSON.stringify(query));

                    if (user.systemHeader && user.systemHeader.deleted) {
                        var deletedMsg = "User is deleted";
                        logger.error(deletedMsg);
                        var error = new Error(deletedMsg);
                        deferred.reject(error);
                    } else {
                        deferred.resolve(user);
                    }
                } else {
                    deferred.reject(new Error("User not found."));
                }
            }

        });

    return deferred.promise;
};

const getIncludeDocs = (userId, includeNames, options) => {
    options = options ? options : {};
    const query = DBProvider.ruleDataService.getIncludeDocsQuery(includeNames);
    return findService.findAll(query, userId, null, null, options);
};

const getRuleSetId = (userId, event, templateId, options) => {
    options = options ? options : {};
    const query = DBProvider.ruleDataService.getRuleSetIdQuery(event, templateId);
    return findService.findAll(query, userId, null, null, options);
};

const getRuleSetIdByName = (userId, name, options) => {
    options = options ? options : {};
    const query = DBProvider.ruleDataService.getRuleSetIdByNameQuery(name);
    return findService.findAll(query, userId, null, null, options);
};

////////////////////////////////////
// public api
module.exports = {

    processUnactionedSystemActions: processUnactionedSystemActions,

    getCurrentScheduledDocuments: getCurrentScheduledDocuments,

    markSystemActionDone,

    getUser:  getUser,

    getIncludeDocs: getIncludeDocs,

    getRuleSetId: getRuleSetId,

    getRuleSetIdByName
};

