// status.js
const express = require('express');
const router = express.Router();
const redisClient = require('../../../../redisClient');

router.get('/statuses', async (req, res) => {
    const cacheKey = 'api_status';

    try {
        // Cek data dari cache Redis dulu
        const cachedData = await redisClient.get(cacheKey);
        if (cachedData) {
            return res.status(200).json(JSON.parse(cachedData));
        }

        // Jika tidak ada di cache, buat response baru dan simpan ke cache selama 60 detik
        const freshData = {
            success: true,
            message: 'Connected!',
            timestamp: new Date().toISOString()
        };

        await redisClient.setEx(cacheKey, 60, JSON.stringify(freshData));

        return res.status(200).json(freshData);

    } catch (error) {
        console.error(error);
        return res.status(500).json({
            success: false,
            message: 'Not connected, something went wrong!',
            error: error.message,
        });
    }
});

module.exports = router;
