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

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

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

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

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

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

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

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

module.exports = billableRouter;