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

const validateEntityScopedListQuery = validateListQuery({ requireEntId: true });

pentityRouter.use(chkLogin, requireUserGroup('User'));

pentityRouter.post('/createpentity', chkLogin, aclCheck("create", "Payee Entity"), async (req, res) => {
    new models.PEntity().createPEntity(req.body, req.session.userId).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error adding Payee Entity data",
                    result.errors,
                    req,
                    false
                )
            )
        }
        return res.json(
            resp(
                "Payee Entity created",
                "Payee Entity added sucessfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error adding Payee Entity data",
                err,
                req
            )
        )
    })
});

pentityRouter.post('/getpentitynames', chkLogin, aclCheck("read", "Payee Entity"), async (req, res) => {
    new models.PEntity().getPEntity(req.body.ent_id).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error returning Payee Entity Names",
                    result.errors,
                    req,
                    false
                )
            )
        }
        return res.json(
            resp(
                "Payee Entity Names",
                "List of Payee Entity Id and Names",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error returning Payee Entity Names",
                err,
                req
            )
        )
    })
});

pentityRouter.post('/readpentities', chkLogin, aclCheck("read", "Payee Entity"), validateEntityScopedListQuery, async (req, res) => {
    new models.PEntity().readPEntity(req.body.col, req.body.dir, req.body.offset, req.body.limit, req.body.ent_id, req.body.search).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error listing Payee Entity",
                    result.errors,
                    req,
                    false
                )
            )
        }
        new models.PEntity().countPEntity(req.body.ent_id).then((count) => {
            return res.json(
                resp(
                    "Payee Entity Data",
                    "List of Payee Entity",
                    {rows: result, rowCount: count},
                    req
                )
            )
        })
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Payee Entity",
                err,
                req
            )
        )
    })
});

pentityRouter.post('/readpentityrow', chkLogin, aclCheck("read", "Payee Entity"), async (req, res) => {
    new models.PEntity().readPEntityRow(req.body).then((result) => {
        return res.json(
            resp(
                "Payee Entity Row Data",
                `Payee Entity with ID ${req.body.id}`,
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                `Error finding Payee Entity with ID ${req.body.id}`,
                err,
                req
            )
        )
    })
});

pentityRouter.put('/updatepentity', chkLogin, aclCheck("update", "Payee Entity"), async (req, res) => {
    new models.PEntity().updatePEntity(req.body).then((result) => {
        return res.json(
            resp(
                "Payee Entity updated",
                "Payee Entity updated successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error updating Payee Entity data",
                err,
                req
            )
        )
    })
});

pentityRouter.delete('/deletepentity', chkLogin, aclCheck("delete", "Payee Entity"), async (req, res) => {
    new models.PEntity().deletePEntity(req.body).then((result) => {
        return res.json(
            resp(
                "Payee Entity Deleted",
                "Payee Entity deleted successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error deleting Payee Entity data",
                err,
                req
            )
        )
    })
});

module.exports = pentityRouter;