const express = require("express");
const router = express.Router();
const multer = require("multer");
const authorize = require("../../../../middlewares/authorize-sapps");
const medicineService = require("./medicine.service");

// setting storage engine
const storage = multer.diskStorage({
	destination: function (req, file, cb) {
		cb(null, "D:/Development/BELAJAR/APPS/SAPPS-BACKEND/public/data/medicines/");
	},
	filename: function (req, file, cb) {
		cb(null, file.originalname);
	},
});

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

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

module.exports = router;

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

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

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

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

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

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