const express = require('express');
const router = express.Router();
const multer = require('multer');
const authorize = require('../../../../middlewares/authorize-sapps');
const dosageTypeService = require('./dosageType.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.delete('/:id', authorize(), _delete);

module.exports = router;

/**
 * Create a new dosageType
 * @param {Object} req.body - The dosageType data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created dosageType
 */
function create(req, res, next) {
    dosageTypeService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

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

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

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

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

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