const express = require('express');
var moduleRouter = express.Router();
const models = require('../models/index.js');
const chkLogin = require('../middleware/auth.js');
const aclCheck = require('../middleware/aclCheck.js');
const validateListQuery = require('../middleware/validateListQuery.js');
var resp = require('../response.js');

const validateStandardListQuery = validateListQuery();

moduleRouter.post('/createmodule', chkLogin, aclCheck("create", "Module"), async (req, res) => {
    new models.Module().createModule(req.body).then((result) => {
        return res.json(
            resp(
                "Module created",
                "Module added sucessfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error adding Module data",
                err,
                req
            )
        )
    })
});

moduleRouter.post('/readmodule', chkLogin, aclCheck("read", "Module"), validateStandardListQuery, async (req, res) => {
    new models.Module().readModule(req.body.col, req.body.dir, req.body.offset, req.body.limit).then((result) => {
        return res.json(
            resp(
                "Module Data",
                "List of Modules",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Modules",
                err,
                req
            )
        )
    })
});

moduleRouter.post('/readmodulerow', chkLogin, aclCheck("read", "Module"), async (req, res) => {
    new models.Module().readModuleRow(req.body).then((result) => {
        return res.json(
            resp(
                "Module Row Data",
                `Module with ID ${req.body.id}`,
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                `Error finding Module with ID ${req.body.id}`,
                err,
                req
            )
        )
    })
});

moduleRouter.put('/updatemodule', chkLogin, aclCheck("update", "Module"), async (req, res) => {
    new models.Module().updateModule(req.body).then((result) => {
        return res.json(
            resp(
                "Module updated",
                "Module updated successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error updating Module data",
                err,
                req
            )
        )
    })
});

moduleRouter.delete('/deletemodule', chkLogin, aclCheck("delete", "Module"), async (req, res) => {
    new models.Module().deleteModule(req.body).then((result) => {
        return res.json(
            resp(
                "Module Deleted",
                "Module deleted successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error deleting Module data",
                err,
                req
            )
        )
    })
});

module.exports = moduleRouter;