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

router.post("/create", authorize(), create);
router.post("/", authorize(), getAll);
router.post("/option", authorize(), option);
router.post("/holiday-list-item-by-date", authorize(), getHolidayListItemByDate);
router.post("/national-holidays", authorize(), getNationalHolidays);
router.get("/count", authorize(), count);
router.put("/:id", authorize(), update);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

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

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

function getHolidayListItemByDate(req, res, next) {
	holidayListService
		.getHolidayListItemByDate(req.body)
		.then((result) => res.json(result))
		.catch(next);
}

/**
 * Fetch national holidays data (server-side proxy + cache) to avoid browser CORS/network issues.
 * Body: { year: number|string, country?: string }
 */
function getNationalHolidays(req, res, next) {
	holidayListService
		.getNationalHolidays(req.body)
		.then((result) => res.json(result))
		.catch(next);
}

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

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

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