const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const pulseAgeRateService = require('./pulseAgeRate.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 pulseAgeRate
 * @param {Object} req.body - The pulseAgeRate data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created pulseAgeRate
 */
function create(req, res, next) {
    pulseAgeRateService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

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

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

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

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

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

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