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

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

module.exports = router;

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

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

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

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

async function option(req, res, next) {
	try {
		// Membuat cacheKey
		const cacheKey = `territories-options`;

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

		// 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 option(req, res, next) {
//     territoryService.option()
//         .then(result => res.json(result))
//         .catch(next);
// }

async function optionParent(req, res, next) {
	try {
		// Membuat cacheKey
		const cacheKey = `territories-options-parents`;

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

		// 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 optionParent(req, res, next) {
//     territoryService.optionParent()
//         .then(result => res.json(result))
//         .catch(next);
// }

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

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

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

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