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

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

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

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

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

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

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