/**
 * Single source of truth for rooms and lights.
 * Lives on the Node-RED server at /root/.node-red/room-config.js
 * All function nodes use: global.get('roomConfig')
 * Loaded at startup from settings.js (functionGlobalContext). To reload without restart, use the
 * "Reload room config" flow: see node-red/reload-room-config-docs.md
 */

const ROOM_CONFIG = {
  // Order must match CODESYS NVL_In. Used by NVL SEND (state-to-nvl-send-payload).
  roomNames: [
    'open_plan_living_room',
    'open_plan_dining_room',
    'open_plan_entrance',
    'open_plan_guest_wc',
    'kitchen_kitchen',
    'kitchen_pantry',
    'bedrooms_office',
    'bedrooms_hallway',
    'bedrooms_laundry',
    'bedrooms_shower',
    'bedrooms_bedroom',
    'master_bedroom_suite',
    'master_bedroom_bathroom',
    'exterior_veranda',
    'exterior_entrance',
    'exterior_yard',
  ],

  // PLC → HA sync. room = key in nvl-receive payload, light = 1..6, entityId = HA entity.
  lightEntityMap: [
    { room: 'l_open_plan_living_room', light: 1, entityId: 'input_boolean.open_plan_living_room_main_1' },
    { room: 'l_open_plan_living_room', light: 2, entityId: 'input_boolean.open_plan_living_room_spots_1' },
    { room: 'l_open_plan_dining_room', light: 1, entityId: 'input_boolean.open_plan_dining_room_main_1' },
    { room: 'l_open_plan_dining_room', light: 2, entityId: 'input_boolean.open_plan_dining_room_spots_1' },
    { room: 'l_open_plan_entrance', light: 1, entityId: 'input_boolean.open_plan_entrance_mirror_1' },
    { room: 'l_open_plan_entrance', light: 2, entityId: 'input_boolean.open_plan_entrance_main_1' },
    { room: 'l_open_plan_guest_wc', light: 1, entityId: 'input_boolean.open_plan_guest_wc_main_1' },
    { room: 'l_open_plan_guest_wc', light: 2, entityId: 'input_boolean.open_plan_guest_wc_strip_1' },
    { room: 'l_open_plan_guest_wc', light: 3, entityId: 'input_boolean.open_plan_guest_wc_fan_1' },
    { room: 'l_open_plan_living_room', light: 3, entityId: 'input_boolean.open_plan_living_room_strip_1' },
    { room: 'l_open_plan_living_room', light: 4, entityId: 'input_boolean.open_plan_living_room_strip_2' },
    { room: 'l_kitchen_kitchen', light: 1, entityId: 'input_boolean.kitchen_kitchen_main_1' },
    { room: 'l_kitchen_kitchen', light: 2, entityId: 'input_boolean.kitchen_kitchen_island_1' },
    { room: 'l_kitchen_kitchen', light: 3, entityId: 'input_boolean.kitchen_kitchen_cabinets_1' },
    { room: 'l_kitchen_kitchen', light: 4, entityId: 'input_boolean.kitchen_kitchen_fridge_1' },
    { room: 'l_kitchen_pantry', light: 1, entityId: 'input_boolean.kitchen_pantry_main_1' },
    { room: 'l_bedrooms_office', light: 1, entityId: 'input_boolean.bedrooms_office_main_1' },
    { room: 'l_bedrooms_office', light: 2, entityId: 'input_boolean.bedrooms_office_strip_1' },
    { room: 'l_bedrooms_hallway', light: 1, entityId: 'input_boolean.bedrooms_hallway_main_1' },
    { room: 'l_bedrooms_laundry', light: 1, entityId: 'input_boolean.bedrooms_laundry_main_1' },
    { room: 'l_bedrooms_shower', light: 1, entityId: 'input_boolean.bedrooms_shower_spots_1' },
    { room: 'l_bedrooms_shower', light: 2, entityId: 'input_boolean.bedrooms_shower_fan_1' },
    { room: 'l_bedrooms_bedroom', light: 1, entityId: 'input_boolean.bedrooms_bedroom_main_1' },
    { room: 'l_bedrooms_bedroom', light: 2, entityId: 'input_boolean.bedrooms_bedroom_spots_1' },
    { room: 'l_master_bedroom_suite', light: 1, entityId: 'input_boolean.master_bedroom_suite_main_1' },
    { room: 'l_master_bedroom_suite', light: 2, entityId: 'input_boolean.master_bedroom_suite_spots_1' },
    { room: 'l_master_bedroom_suite', light: 3, entityId: 'input_boolean.master_bedroom_suite_strip_1' },
    { room: 'l_master_bedroom_suite', light: 4, entityId: 'input_boolean.master_bedroom_suite_door_1' },
    { room: 'l_master_bedroom_suite', light: 5, entityId: 'input_boolean.master_bedroom_suite_dresser_1' },
    { room: 'l_master_bedroom_bathroom', light: 1, entityId: 'input_boolean.master_bedroom_bathroom_spots_1' },
    { room: 'l_master_bedroom_bathroom', light: 2, entityId: 'input_boolean.master_bedroom_bathroom_fan_1' },
    { room: 'l_exterior_veranda', light: 1, entityId: 'input_boolean.exterior_veranda_main_1' },
    { room: 'l_exterior_yard', light: 1, entityId: 'input_boolean.exterior_yard_bbq_1' },
    { room: 'l_exterior_entrance', light: 1, entityId: 'input_boolean.exterior_entrance_door_1' },
    { room: 'l_exterior_yard', light: 2, entityId: 'input_boolean.exterior_yard_front_lights_1' },
    { room: 'l_exterior_yard', light: 3, entityId: 'input_boolean.exterior_yard_backyard_flood_light_1' },
    { room: 'l_exterior_yard', light: 4, entityId: 'input_boolean.exterior_yard_backyard_flood_light_2' },
    { room: 'l_exterior_yard', light: 5, entityId: 'input_boolean.exterior_yard_master_bedroom_flood_light_1' },
    { room: 'l_exterior_yard', light: 6, entityId: 'input_boolean.exterior_yard_driveway_1' },
  ],

  // HA entity_id substring (after domain.) without trailing _N → NVL room key. For HA to NVL.
  entityToRoom: {
    open_plan_living_room_main: 'open_plan_living_room',
    open_plan_living_room_spots: 'open_plan_living_room',
    open_plan_living_room_strip: 'open_plan_living_room',
    open_plan_dining_room_main: 'open_plan_dining_room',
    open_plan_dining_room_spots: 'open_plan_dining_room',
    open_plan_entrance_mirror: 'open_plan_entrance',
    open_plan_entrance_main: 'open_plan_entrance',
    open_plan_guest_wc_main: 'open_plan_guest_wc',
    open_plan_guest_wc_strip: 'open_plan_guest_wc',
    open_plan_guest_wc_fan: 'open_plan_guest_wc',
    kitchen_kitchen_main: 'kitchen_kitchen',
    kitchen_kitchen_island: 'kitchen_kitchen',
    kitchen_kitchen_cabinets: 'kitchen_kitchen',
    kitchen_kitchen_fridge: 'kitchen_kitchen',
    kitchen_pantry_main: 'kitchen_pantry',
    bedrooms_office_main: 'bedrooms_office',
    bedrooms_office_strip: 'bedrooms_office',
    bedrooms_hallway_main: 'bedrooms_hallway',
    bedrooms_laundry_main: 'bedrooms_laundry',
    bedrooms_shower_spots: 'bedrooms_shower',
    bedrooms_shower_fan: 'bedrooms_shower',
    bedrooms_bedroom_main: 'bedrooms_bedroom',
    bedrooms_bedroom_spots: 'bedrooms_bedroom',
    master_bedroom_suite_main: 'master_bedroom_suite',
    master_bedroom_suite_spots: 'master_bedroom_suite',
    master_bedroom_suite_strip: 'master_bedroom_suite',
    master_bedroom_suite_door: 'master_bedroom_suite',
    master_bedroom_suite_dresser: 'master_bedroom_suite',
    master_bedroom_bathroom_spots: 'master_bedroom_bathroom',
    master_bedroom_bathroom_fan: 'master_bedroom_bathroom',
    exterior_veranda_main: 'exterior_veranda',
    exterior_yard_bbq: 'exterior_yard',
    exterior_entrance_door: 'exterior_entrance',
    exterior_yard_front_lights: 'exterior_yard',
    exterior_yard_backyard_flood_light: 'exterior_yard',
    exterior_yard_master_bedroom_flood_light: 'exterior_yard',
    exterior_yard_driveway: 'exterior_yard',
  },

  // // Zigbee: friendly_name → single room (fallback when switchBindings missing).
  // deviceToRoom: {
  //   'Office Switch': 'open_plan_living_room',
  // },

  // When using a multi-device Zigbee node, payload is keyed by IEEE (e.g. 0xa4c138a5b9771b05). Map IEEE → friendly name so switchBindings by name still works.
  deviceIdToName: {
    // '0xa4c138a5b9771b05': 'Office Switch',   // uncomment and add your device IEEE from the payload
  },

  // Optional: bind by IEEE instead of friendly name (same shape as switchBindings). Use if the node never sends friendly_name.
  // switchBindingsByDeviceId: {
  //   '0xa4c138a5b9771b05': { 1: { room: 'open_plan_living_room', light: 1 }, 2: { room: 'open_plan_dining_room', light: 1 } },
  // },

  /**
   * Which (room, light) each physical button controls. Key = button number (1..6), value = { room, light }.
   * Button and light are independent: e.g. button 2 can control light 1, or light 2 in another room.
   * One button can target one { room, light } or an array for multiple lights.
   */
  switchBindings: {
    'Office Switch': {
      1: { room: 'open_plan_living_room', light: 1 },
      2: { room: 'open_plan_dining_room', light: 1 },
    },
  },
};

module.exports = ROOM_CONFIG;
