const path = require("path");
const fs = require("fs");

function SetAdminValuesMiddleware(req, res, next){
    try{
        const configFilePath = path.join(__dirname, '..', 'db', 'config.json');
        const config = JSON.parse(fs.readFileSync(configFilePath));

        res.locals.adminKey = config.adminKey === undefined ? " " : config.adminKey;
        res.locals.ceoID = config.ceoID === undefined ? " " : config.ceoID;

        next();

        fs.writeFileSync(configFilePath, JSON.stringify(
            {
                adminKey: res.locals.adminKey,
                ceoID: res.locals.ceoID
            }, null, 2));
        return res;
    } catch(error){
        console.log(error.message);
        return res;
    }
}

module.exports = SetAdminValuesMiddleware;