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

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

accountRouter.post('/createaccount', chkLogin, aclCheck("create", "Bank Account"), async (req, res) => {
    new models.BankAccount().createAccount(req.body, req.session.userId).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error adding Bank Account data",
                    err,
                    req
                )
            )
        }
        return res.json(
            resp(
                "Bank Account created",
                "Bank Account added sucessfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error adding Bank Account data",
                err,
                req
            )
        )
    })
});

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

accountRouter.post('/readaccounts', chkLogin, aclCheck("read", "Bank Account"), validateStandardListQuery, async (req, res) => {
    new models.BankAccount().readAccount(req.body.col, req.body.dir, req.body.offset, req.body.limit, req.body.search).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error listing Bank Accounts",
                    result.errors,
                    req,
                    false
                )
            )    
        }
        new models.BankAccount().countAccounts().then((count) => {
            return res.json(
                resp(
                    "Bank Account Data",
                    "List of Bank Accounts",
                    {rows: result, rowCount: count},
                    req
                )
            )
        })
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Bank Accounts",
                err,
                req
            )
        )
    })
});

accountRouter.post('/readaccountrow', chkLogin, aclCheck("read", "Bank Account"), async (req, res) => {
    new models.BankAccount().readAccountRow(req.body).then((result) => {
        return res.json(
            resp(
                "Bank Account Row Data",
                `Bank Account with ID ${req.body.id}`,
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                `Error finding Bank Account with ID ${req.body.id}`,
                err,
                req
            )
        )
    })
});

accountRouter.put('/updateaccount', chkLogin, aclCheck("update", "Bank Account"), async (req, res) => {
    new models.BankAccount().updateAccount(req.body).then((result) => {
        return res.json(
            resp(
                "Bank Account updated",
                "Bank Account updated successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error updating Bank Account data",
                err,
                req
            )
        )
    })
});

accountRouter.delete('/deleteaccount', chkLogin, aclCheck("delete", "Bank Account"), async (req, res) => {
    new models.BankAccount().deleteAccount(req.body).then((result) => {
        return res.json(
            resp(
                "Bank Account Deleted",
                "Bank Account deleted successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error deleting Bank Account data",
                err,
                req
            )
        )
    })
});

module.exports = accountRouter;