const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const ptkpStatusService = require('./ptkpStatus.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 ptkp status
 * @param {Object} req.body - The ptkp status data
 * @param {Object} res - The response object
 * @param {Function} next - The next middleware function
 * @returns {Promise} - A promise resolving to the created ptkp status
 */
function create(req, res, next) {
    ptkpStatusService.create(req.body)
        .then(result => res.json(result))
        .catch(next);
}

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

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

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

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

/**
 * Enable or disable a ptkp status
 * @param {number} req.params.id - The ptkp status ID to be enabled/disabled
 * @param {Object} req.body - The ptkp status data to be updated
 * @param {boolean} req.body.enabled - The enabled status of the ptkp status
 * @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) {
    ptkpStatusService.enabled(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

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