const express = require("express");
const router = express.Router();
const multer = require("multer");
const authorize = require("../../../../middlewares/authorize-sapps");
const bankAccountService = require("./bankAccount.service");

router.post("/create", authorize(), create);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.get("/vendors", authorize(), vendor);
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 drug
 * @param {Object} req.body - The drug data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created drug
 */
function create(req, res, next) {
  bankAccountService
    .create(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Get a list of all drugs
 * @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 drugs
 */
function getAll(req, res, next) {
  bankAccountService
    .getAll(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Get an array of drug 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 drug objects
 */
function option(req, res, next) {
  bankAccountService
    .option(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Retrieve vendor information
 * @param {Object} req.query - The query object containing vendor parameters
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function in the stack
 * @returns {Promise} - A promise resolving to the vendor information
 */
function vendor(req, res, next) {
  bankAccountService
    .getVendor(req.query)
    .then((result) => res.json(result)) // Send the retrieved vendor information as JSON response
    .catch(next); // Pass any errors to the next middleware
}

/**
 * Count the number of drugs 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 drugs
 */
function count(req, res, next) {
  bankAccountService
    .count(req.query)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Update an existing drug
 * @param {number} req.params.id - The drug ID to be updated
 * @param {Object} req.body - The drug 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 drug
 */
function update(req, res, next) {
  bankAccountService
    .update(req.params.id, req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function enabled(req, res, next) {
  bankAccountService
    .enabled(req.params.id, req.body)
    .then((result) => res.json(result))
    .catch(next);
}

function _delete(req, res, next) {
  bankAccountService
    .delete(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}
