const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const religionService = require("./religion.service");

router.post("/create", authorize(), create);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.post("/option-free", option);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

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

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

/**
 * Enable or disable a religion
 * @param {number} req.params.id - The religion ID to be enabled/disabled
 * @param {Object} req.body - The religion data to be updated
 * @param {boolean} req.body.enabled - The enabled status of the religion
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the result of the enable/disable process
 */
function enabled(req, res, next) {
	religionService
		.enabled(req.params.id, req.body)
		.then((result) => res.json(result))
		.catch(next);
}

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