// Paste into a Function node named "Reload room config from payload".
// Input: msg.payload = JSON string (from Exec node that runs the script below).
// Sets global roomConfig so all flows see the new config without restarting Node-RED.

const raw = msg.payload;
if (typeof raw !== 'string') {
  node.warn('[Reload config] expected string payload, got ' + typeof raw);
  return null;
}
try {
  const config = JSON.parse(raw.trim());
  if (!config.roomNames || !Array.isArray(config.roomNames)) {
    node.error('[Reload config] invalid config: missing roomNames');
    return null;
  }
  global.set('roomConfig', config);
  node.warn('[Reload config] OK: ' + config.roomNames.length + ' rooms, ' + (config.lightEntityMap ? config.lightEntityMap.length : 0) + ' light mappings');
  return msg;
} catch (e) {
  node.error('[Reload config] parse error: ' + e.message);
  return null;
}
