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

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

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

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

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

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