// src/mqtt/publishers.js
function createPublishers(client) {
    /**
     * ✅ Firmware novo:
     * Para settings: <id>/cmd/evse/settings
     * payload: { currentLimit, temperatureLimit }
     */
    function sendEvseSettings(chargerTopic, settings = {}) {
      const payload = {};
      if (settings.currentLimit !== undefined) payload.currentLimit = Number(settings.currentLimit);
      if (settings.temperatureLimit !== undefined)
        payload.temperatureLimit = Number(settings.temperatureLimit);
  
      if (!Object.keys(payload).length) return;
  
      client.publish(`${chargerTopic}/cmd/evse/settings`, JSON.stringify(payload), { qos: 1 });
    }
  
    // compat + mapeamento
    function sendConfig(chargerTopic, property, value) {
      const map = {
        maxChargingCurrent: 'currentLimit',
        temperatureThreshold: 'temperatureLimit',
      };
  
      if (map[property]) {
        return sendEvseSettings(chargerTopic, { [map[property]]: value });
      }
  
      // fallback legacy
      const payload = { [property]: value };
      client.publish(`${chargerTopic}/set/config/evse`, JSON.stringify(payload), { qos: 1 });
    }
  
    // legacy (mantidos)
    function sendEnable(chargerTopic, enable) {
      client.publish(`${chargerTopic}/enable`, JSON.stringify({ enable: !!enable }), { qos: 1 });
    }
  
    function requestConfig(chargerTopic) {
      client.publish(`${chargerTopic}/request/config/evse`, null, { qos: 1 });
    }
  
    // helper genérico novo
    function sendCmd(chargerTopic, cmdSubtopic, obj) {
      const t = `${chargerTopic}/cmd/${cmdSubtopic}`;
      const msg = obj ? JSON.stringify(obj) : '';
      client.publish(t, msg, { qos: 1 });
    }
  
    return { sendConfig, sendEvseSettings, sendEnable, requestConfig, sendCmd };
  }
  
  module.exports = { createPublishers };
  