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

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

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

branchRouter.post('/readbranches', chkLogin, aclCheck("read", "Bank Branch"), validateStandardListQuery, async (req, res) => {
    new models.BankBranch().readBranch(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 Branches",
                    result.errors,
                    req,
                    false
                )
            )
        }
        new models.BankBranch().countBranch(req.body.id).then((count) => {
            return res.json(
                resp(
                    "Bank Branch Data",
                    "List of Banks",
                    {rows: result, rowCount: count},
                    req
                )
            )
        })
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Bank Branches",
                err,
                req
            )
        )
    })
});

branchRouter.post('/readbranchrow', chkLogin, aclCheck("read", "Bank Branch"), async (req, res) => {
    new models.BankBranch().readBranchRow(req.body).then((result) => {
        return res.json(
            resp(
                "Bank Branch Row Data",
                `Bank Branch with ID ${req.body.id}`,
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                `Error finding Bank Branch with ID ${req.body.id}`,
                err,
                req
            )
        )
    })
});

branchRouter.put('/updatebranch', chkLogin, aclCheck("update", "Bank Branch"), async (req, res) => {
    new models.BankBranch().updateBranch(req.body).then((result) => {
        return res.json(
            resp(
                "Bank Branch updated",
                "Bank Branch updated successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error updating Bank Branch data",
                err,
                req
            )
        )
    })
});

branchRouter.delete('/deletebranch', chkLogin, aclCheck("delete", "Bank Branch"), async (req, res) => {
    new models.BankBranch().deleteBranch(req.body).then((result) => {
        return res.json(
            resp(
                "Bank Branch Deleted",
                "Bank Branch deleted successfully",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error deleting Bank Branch data",
                err,
                req
            )
        )
    })
});

branchRouter.post('/listbranches', chkLogin, aclCheck("read", "Bank Branch"), async (req, res) => {
    new models.BankBranch().getBranchesList(req.body.filter.bank_id).then((result) => {
        if(result.errors) {
            return res.json(
                resp(
                    "Error",
                    "Error listing Bank Branch",
                    result.errors,
                    req,
                    false
                )
            )    
        }
        return res.json(
            resp(
                "Bank Branch Data",
                "List of Bank Branches",
                result,
                req
            )
        )
    }).catch((err) => {
        return res.json(
            resp(
                "Error",
                "Error listing Bank Branch",
                err,
                req,
                false
            )
        )
    })
});

module.exports = branchRouter;