const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const salesPlanService = require("./salesPlan.service");
const redisClient = require('../../../../redisClient');

router.post("/create", authorize(), create);
router.post("/generate-doc-no", authorize(), generateDocumentNo);
router.post("/generate-estimate", authorize(), generateEstimate);
router.post("/generate-realization", authorize(), generateRealization);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.get("/item-plan/:id", authorize(), getSalesItemPlanById);
router.get("/estimate/:id", authorize(), getEstimateByPlanId);
router.get("/realization/:id", authorize(), getRealizationByPlanId);
router.put("/:id", authorize(), update);
router.put("/enabled/:id", authorize(), enabled);
router.put("/estimate/:id", authorize(), updateEstimate);
router.delete("/:id", authorize(), _delete);
router.delete("/estimate/:id", authorize(), deleteEstimate);

module.exports = router;

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

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

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

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

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

async function option(req, res, next) {
    try {
        // Membuat cacheKey
        const cacheKey = `sales-plans-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 salesPlanService.option()
        const result = await salesPlanService.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) {
//   salesPlanService
//     .option()
//     .then((result) => res.json(result))
//     .catch(next);
// }

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

async function getSalesItemPlanById(req, res, next) {
    try {
        const result = await salesPlanService.getSalesItemPlanById(req.params.id);
        return res.json(result);
    } catch (error) {
        return next(error);
    }
}

async function getEstimateByPlanId(req, res, next) {
    try {
        const result = await salesPlanService.getEstimateByPlanId(req.params.id);
        return res.json(result);
    } catch (error) {
        return next(error);
    }
}

async function getRealizationByPlanId(req, res, next) {
    try {
        const result = await salesPlanService.getRealizationByPlanId(req.params.id);
        return res.json(result);
    } catch (error) {
        return next(error);
    }
}

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

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

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

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

function deleteEstimate(req, res, next) {
    salesPlanService
        .deleteEstimate(req.params.id)
        .then((result) => res.json(result))
        .catch(next);
}

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