const express = require('express');
var groupRouter = 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();

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

groupRouter.post('/readgroup', chkLogin, aclCheck("read", "Group"), validateStandardListQuery, async (req, res) => {
    new models.Group().readGroup(req.body.col, req.body.dir, req.body.offset, req.body.limit).then((result) => {
        return res.json(
            resp(
                "Group Data",
                "List of Groups",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Groups",
                err,
                req
            )
        )
    })
});

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

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

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

module.exports = groupRouter;