const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const bankService = require('./bank.service');

router.post('/create', authorize(), create);
router.post('/', authorize(), getAll);
router.get('/option', authorize(), option);
router.get('/count', authorize(), count);
router.put('/:id', authorize(), update);
router.put('/enabled/:id', authorize(), enabled);
router.delete('/:id', authorize(), _delete);

module.exports = router;

/**
 * Create a new bank
 * @param {Object} req.body - The bank data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created bank
 */
function create(req, res, next) {
    bankService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Get a list of all banks
 * @param {Object} req.query - The query object
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to an array of banks
 */
function getAll(req, res, next) {
    bankService.getAll(req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Get an array of bank objects to be used as options in a select box
 * @param {Object} req.query - The query object
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to an array of bank objects
 */
function option(req, res, next) {
    bankService.option(req.query)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Count the number of banks matching a query
 * @param {Object} req.query - The query object
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the count of banks
 */
function count(req, res, next) {
    bankService.count(req.query)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Update an existing bank
 * @param {number} req.params.id - The bank ID to be updated
 * @param {Object} req.body - The bank data to be updated
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the updated bank
 */
function update(req, res, next) {
    bankService.update(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Enable or disable a bank
 * @param {number} req.params.id - The bank ID to be enabled/disabled
 * @param {Object} req.body - The bank data to be updated
 * @param {boolean} req.body.enabled - The enabled status of the bank
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the result of the enable/disable process
 */
function enabled(req, res, next) {
    bankService.enabled(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

/**
 * Delete a bank
 * @param {number} req.params.id - The bank ID to be deleted
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the result of the deletion process
 */
function _delete(req, res, next) {
    bankService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}
