const express = require('express');
const router = express.Router();
const { pool } = require('../config/database');
const { authenticateToken, authorizePermission } = require('../middleware/auth');
/**
 * @route   POST /api/utility/populate-last-seen
 * @desc    Populate last_seen for all active users (for testing)
 * @access  Super Admin only
 */
router.post('/populate-last-seen', authenticateToken, authorizePermission('admin.access'), async (req, res) => {
    try {
        const now = new Date().toISOString().slice(0, 19).replace('T', ' ');
        // Update all active users without last_seen
        const [result] = await pool.execute('UPDATE users SET last_seen = ? WHERE is_active = 1 AND last_seen IS NULL', [now]);
        // Get count of users with last_seen
        const [countResult] = await pool.execute('SELECT COUNT(*) as count FROM users WHERE is_active = 1 AND last_seen IS NOT NULL');
        res.status(200).json({
            status: 'success',
            message: 'Last seen data populated successfully',
            data: {
                updated_users: result.affectedRows,
                total_users_with_last_seen: countResult[0].count
            }
        });
    }
    catch (error) {
        console.error('Error populating last_seen:', error);
        res.status(500).json({
            status: 'error',
            message: 'Failed to populate last_seen data',
            error: error.message
        });
    }
});
/**
 * @route   GET /api/utility/active-users-count
 * @desc    Get count of active users with last_seen data
 * @access  Authenticated users
 */
router.get('/active-users-count', authenticateToken, async (req, res) => {
    try {
        const [result] = await pool.execute('SELECT COUNT(*) as count FROM users WHERE is_active = 1 AND last_seen IS NOT NULL');
        res.status(200).json({
            status: 'success',
            data: {
                active_users_count: result[0].count
            }
        });
    }
    catch (error) {
        console.error('Error getting active users count:', error);
        res.status(500).json({
            status: 'error',
            message: 'Failed to get active users count',
            error: error.message
        });
    }
});
module.exports = router;
//# sourceMappingURL=utility.js.map