const test = require('node:test');
const assert = require('node:assert/strict');

const ConfigUtils = require('../src/helper/configUtils.js');

const defaultConfig = {
  functionality: {
    softwareType: {
      default: 'measurement',
      rules: { type: 'string' },
    },
  },
  general: {
    logging: {
      enabled: { default: true, rules: { type: 'boolean' } },
      logLevel: {
        default: 'info',
        rules: {
          type: 'enum',
          values: [{ value: 'debug' }, { value: 'info' }, { value: 'warn' }, { value: 'error' }],
        },
      },
    },
    name: { default: 'default-name', rules: { type: 'string' } },
  },
  scaling: {
    absMin: { default: 0, rules: { type: 'number' } },
    absMax: { default: 100, rules: { type: 'number' } },
  },
};

test('initConfig applies defaults', () => {
  const cfg = new ConfigUtils(defaultConfig, false, 'error');
  const result = cfg.initConfig({});
  assert.equal(result.general.name, 'default-name');
  assert.equal(result.scaling.absMax, 100);
});

test('updateConfig merges nested overrides and revalidates', () => {
  const cfg = new ConfigUtils(defaultConfig, false, 'error');
  const base = cfg.initConfig({ general: { name: 'sensor-a' } });
  const updated = cfg.updateConfig(base, { scaling: { absMax: 150 } });

  assert.equal(updated.general.name, 'sensor-a');
  assert.equal(updated.scaling.absMax, 150);
});

test('constructor respects explicit logger disabled flag', () => {
  const cfg = new ConfigUtils(defaultConfig, false, 'error');
  assert.equal(cfg.logger.logging, false);
});
