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

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

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

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

taxRouter.post('/readtaxes', chkLogin, aclCheck("read", "Tax"), validateEntityScopedListQuery, async (req, res) => {
    new models.Tax().readTax(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 Taxes",
                    result.errors,
                    req,
                    false
                )
            )
        }
        new models.Tax().countTax(req.body.ent_id).then((count) => {
            return res.json(
                resp(
                    "Tax Data",
                    "List of Taxes",
                    {rows: result, rowCount: count},
                    req
                )
            )
        })
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Tax",
                err,
                req
            )
        )
    })
});

taxRouter.post('/readtaxrow', chkLogin, aclCheck("read", "Tax"), async (req, res) => {
    new models.Tax().readTaxRow(req.body).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    `Error finding Tax with ID ${req.body.id}`,
                    err,
                    req,
                    false
                )
            )   
        }
        return res.json(
            resp(
                "Tax Row Data",
                `Tax with ID ${req.body.id}`,
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                `Error finding Tax with ID ${req.body.id}`,
                err,
                req
            )
        )
    })
});

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

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

module.exports = taxRouter;