const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const gimmickRequestService = require("./gimmickRequest.service");

router.post("/create", authorize(), create);
router.post("/send-email-notification", sendEmailNotification);
router.post("/generate-doc-no", authorize(), generateDocumentNo);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.post("/bulk-update-receiver", authorize(), bulkUpdateReceiver);
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 gimmickRequest
 * @param {Object} req.body - The gimmickRequest data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created gimmickRequest
 */
function create(req, res, next) {
  gimmickRequestService
    .create(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

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

function sendEmailNotification(req, res, next) {
  gimmickRequestService
    .sendEmailNotification(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

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

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

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

/**
 * Enable or disable a gimmickRequest
 * @param {number} req.params.id - The gimmickRequest ID to be enabled/disabled
 * @param {Object} req.body - The gimmickRequest data to be updated
 * @param {boolean} req.body.enabled - The enabled status of the gimmickRequest
 * @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) {
  gimmickRequestService
    .enabled(req.params.id, req.body)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Delete a gimmickRequest
 * @param {number} req.params.id - The gimmickRequest 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) {
  gimmickRequestService
    .delete(req.params.id)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Generate document number for gimmick request
 * @param {Object} req.body - The gimmick request data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the generated document number
 */
function generateDocumentNo(req, res, next) {
  gimmickRequestService
    .generateDocumentNo(req.body)
    .then((result) => res.json(result))
    .catch(next);
}

/**
 * Bulk update receiver of gimmick requests
 * @param {Object} req.body - The gimmick request data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the result of the bulk update process
 */
function bulkUpdateReceiver(req, res, next) {
  gimmickRequestService
    .bulkUpdateReceiver(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
