const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const designationService = require("./designation.service");
const redisClient = require('../../../../redisClient');

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 designation
 * @param {Object} req.body - designation data
 * @returns {Object} - created designation data
 */
function create(req, res, next) {
    designationService
        .create(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

/**
 * Get all designations
 * @param {Object} req - request object
 * @param {Object} res - response object
 * @param {Function} next - next middleware function
 * @returns {Object} - all designation data
 */
function getAll(req, res, next) {
    designationService
        .getAll(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

/**
 * Get designation options
 * @param {Object} req - request object
 * @param {Object} res - response object
 * @param {Function} next - next middleware function
 * @returns {Object} - all designation options
 */
function option(req, res, next) {
    designationService
        .option(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

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

/**
 * Update a designation
 * @param {string} req.params.id - The ID of the designation to update
 * @param {Object} req.body - The new data for the designation
 * @returns {Promise} - A promise resolving to the updated designation
 */
function update(req, res, next) {
    designationService
        .update(req.params.id, req.body)
        .then((result) => res.json(result))
        .catch(next);
}

/**
 * Enable or disable a designation
 * @param {string} req.params.id - The ID of the designation to enable or disable
 * @param {Object} req.body - The new data for the designation
 * @returns {Promise} - A promise resolving to the updated designation
 */
function enabled(req, res, next) {
    designationService
        .enabled(req.params.id, req.body)
        .then((result) => res.json(result))
        .catch(next);
}

/**
 * Delete a designation
 * @param {string} req.params.id - The ID of the designation to delete
 * @returns {Promise} - A promise resolving to the deleted designation
 */
function _delete(req, res, next) {
    designationService
        .delete(req.params.id)
        .then((result) => res.json(result))
        .catch(next);
}
