const express = require('express');
const router = express.Router();
const multer = require('multer');
const authorize = require('../../../../middlewares/authorize-sapps');
const drugService = require('./drug.service');

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/data/medications/');
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});

const upload = multer({ storage: storage });

router.post('/create', authorize(), upload.single('image'), create);
router.post('/', authorize(), getAll);
router.post('/option', authorize(), option);
router.get('/count', authorize(), count);
router.put('/:id', authorize(), upload.single('image'), update);
router.delete('/:id', authorize(), _delete);

module.exports = router;

/**
 * Create a new drug
 * @param {Object} req.body - The drug data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created drug
 */
function create(req, res, next) {
    const reqBody = {
        ...req.body,
        image: req.file ? req.file.filename : null
    };

    drugService.create(reqBody)
        .then(result => res.json(result))
        .catch(next);
}

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

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

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

/**
 * Update an existing drug
 * @param {number} req.params.id - The drug ID to be updated
 * @param {Object} req.body - The drug 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 drug
 */
function update(req, res, next) {
    const reqBody = {
        ...req.body,
        image: req.file ? req.file.filename : req.body.image ? req.body.image : null
    };

    drugService.update(req.params.id, reqBody)
        .then(result => res.json(result))
        .catch(next);
}

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