// internal dependencies
const connections = require('./connections.js'); // connection manager
const stopService = require('./stop.js'); // for server reference

/*
    * Get WebSocket service status
    * Returns:
    * {
    *   online: boolean,
    *   clients: {
    *     connectionId1: { metadata: {...}, lastHeartbeat: ... },
    *     connectionId2: { ... }
    *   }
    * }
*/
module.exports = async function websocketStatus() {
    try {
        // check if server exists and is running
        const serverOnline = !!stopService.getServer();

        // get active clients
        const clients = connections.getAll(); // object with connectionId -> { metadata, lastHeartbeat }

        return {
            online: serverOnline,
            clients
        };
    } catch (err) {
        console.error('Error getting WebSocket status:', err);
        return {
            online: false,
            clients: {},
            error: err
        };
    }
};