const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const workflowService = require("./workflow.service");
const redisClient = require("../../../../redisClient");

router.post("/create", authorize(), create);
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.put("/workflow-cp/:id", authorize(), updateWorkflowCP);
router.put("/submit-to-workflow/:id", authorize(), submitToWorkflow);
router.put("/action-to-workflow/:id", authorize(), actionToWorkflow);
router.delete("/:id", authorize(), _delete);

module.exports = router;

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

async function getAll(req, res, next) {
	try {
		// Membuat cacheKey
		const cacheKey = `workflows`;

		// 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 workflowService.getAll()
		const result = await workflowService.getAll();

		// 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) {
//     workflowService
//         .getAll()
//         .then((result) => res.json(result))
//         .catch(next);
// }

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

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

//         // 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 companyService.getAll(req.query)
//         const result = await workflowService.getByApprover(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 getByApprover(req, res, next) {
	workflowService
		.getByApprover(req.query)
		.then((result) => res.json(result))
		.catch(next);
}

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

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

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

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

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

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

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