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

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

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

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

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

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

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

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