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


router.post('/create', authorize(), create);
router.post('/create/subtype', authorize(), createSubType);
router.post('/', authorize(), getAll);
router.post('/subtype', authorize(), getAllSubType);
router.get('/option', authorize(), option);
router.get('/option/subtype', authorize(), optionSubType);
router.get('/count', authorize(), count);
router.get('/count/subtype', authorize(), countSubType);
router.put('/:id', authorize(), update);
router.put('/subtype/:id', authorize(), updateSubType);
router.put("/enabled/:id", authorize(), TypeEnabled);
router.put("/subtype/enabled/:id", authorize(), SubTypeEnabled);
router.delete('/:id', authorize(), _delete);
router.delete('/subtype/:id', authorize(), deleteSubType);

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) {
    gimmickReqTypeService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

function createSubType(req, res, next) {
    gimmickReqTypeService.createSubType(req.body)
        .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) {
    gimmickReqTypeService.getAll(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getAllSubType(req, res, next) {
    gimmickReqTypeService.getAllSubType(req.query)
        .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) {
    gimmickReqTypeService.option(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function optionSubType(req, res, next) {
    gimmickReqTypeService.optionSubType(req.query)
        .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) {
    gimmickReqTypeService.count(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function countSubType(req, res, next) {
    gimmickReqTypeService.countSubType(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
    };

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

function updateSubType(req, res, next) {
    const reqBody = {
        ...req.body,
        image: req.file ? req.file.filename : req.body.image ? req.body.image : null
    };

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

function TypeEnabled(req, res, next) {
    gimmickReqTypeService
        .TypeEnabled(req.params.id, req.body)
        .then((result) => res.json(result))
        .catch(next);
}

function SubTypeEnabled(req, res, next) {
    gimmickReqTypeService
        .SubTypeEnabled(req.params.id, req.body)
        .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) {
    gimmickReqTypeService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}

function deleteSubType(req, res, next) {
    gimmickReqTypeService.deleteSubType(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}
