const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const departmentService = require("./department.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 department
 * @param {Object} req.body - department data
 * @returns {Object} - created department data
 */
function create(req, res, next) {
    departmentService
        .create(req.body)
        .then((result) => res.json(result))
        .catch(next);
}

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

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

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

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

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

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