const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const absenceClarificationService = require('./absenceClarification.service');
const redisClient = require('../../../../redisClient');

router.post('/create', authorize(), create);
router.post('/create-workflow', authorize(), createWorkflow);
router.get('/', authorize(), getAll);
router.get('/get-by-approver', authorize(), getByApprover);
router.get('/filter', authorize(), filter);
router.get('/option', authorize(), option);
router.get('/count', authorize(), count);
router.put('/:id', authorize(), update);
router.delete('/:id', authorize(), _delete);

module.exports = router;

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

// async function getAll(req, res, next) {
//     try {
//         // Membuat cacheKey berdasarkan req.query
//         const cacheKey = `statuses:${JSON.stringify(req.query)}-absence-clarifications`;

//         // Mengecek apakah data sudah ada di Redis
//         const cachedData = await redisClient.get(cacheKey);
//         if (cachedData) {
//             return res.json(JSON.parse(cachedData));
//         }

//         // Jika tidak ada, panggil absenceClarificationService.getAll(req.query)
//         const result = await absenceClarificationService.getAll(req.query);

//         // Simpan hasilnya ke Redis dengan expire time misalnya 60 detik
//         await redisClient.setEx(cacheKey, 60, JSON.stringify(result));

//         // Kembalikan response JSON dari hasil panggilan service
//         return res.json(result);
//     } catch (error) {
//         return next(error);
//     }
// }


function getAll(req, res, next) {
    absenceClarificationService.getAll(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getByApprover(req, res, next) {
    absenceClarificationService.getByApprover(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function filter(req, res, next) {
    absenceClarificationService.filter(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function option(req, res, next) {
    absenceClarificationService.option(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function count(req, res, next) {
    absenceClarificationService.count()
        .then(result => res.json(result))
        .catch(next);
}

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

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

function _delete(req, res, next) {
    absenceClarificationService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}

