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

const NodeClass = require('../../src/nodeClass');

function loadConfig(uiConfig = {}) {
  const ctx = { name: 'pumpingStation' };
  NodeClass.prototype._loadConfig.call(ctx, {
    name: 'PS Config Test',
    basinVolume: 80,
    basinHeight: 8,
    inflowLevel: 3.2,
    outflowLevel: 0.4,
    overflowLevel: 7.4,
    inletPipeDiameter: 0.5,
    outletPipeDiameter: 0.35,
    refHeight: 'NAP',
    minHeightBasedOn: 'outlet',
    basinBottomRef: -1.2,
    maxInflowRate: 300,
    staticHead: 11,
    maxDischargeHead: 22,
    pipelineLength: 120,
    defaultFluid: 'wastewater',
    temperatureReferenceDegC: 16,
    controlMode: 'levelbased',
    minLevel: 0.8,
    startLevel: 2,
    maxLevel: 6.5,
    levelCurveType: 'log',
    logCurveFactor: 7,
    enableDryRunProtection: true,
    dryRunThresholdPercent: 3,
    enableHighVolumeSafety: true,
    highVolumeSafetyThresholdPercent: 96,
    timeleftToFullOrEmptyThresholdSeconds: 60,
    processOutputFormat: 'process',
    dbaseOutputFormat: 'influxdb',
    ...uiConfig,
  }, { id: 'node-1' });
  return ctx.config;
}

test('nodeClass config mapping — basin, hydraulics, mode and safety fields', () => {
  const cfg = loadConfig();

  assert.equal(cfg.basin.inletPipeDiameter, 0.5);
  assert.equal(cfg.basin.outletPipeDiameter, 0.35);
  assert.equal(cfg.hydraulics.maxInflowRate, 300);
  assert.equal(cfg.hydraulics.staticHead, 11);
  assert.equal(cfg.hydraulics.maxDischargeHead, 22);
  assert.equal(cfg.hydraulics.pipelineLength, 120);
  assert.equal(cfg.hydraulics.defaultFluid, 'wastewater');
  assert.equal(cfg.hydraulics.temperatureReferenceDegC, 16);
  assert.equal(cfg.control.mode, 'levelbased');
  assert.equal(cfg.control.levelbased.curveType, 'log');
  assert.equal(cfg.control.levelbased.logCurveFactor, 7);
  assert.equal(cfg.safety.enableHighVolumeSafety, true);
  assert.equal(cfg.safety.highVolumeSafetyThresholdPercent, 96);
  assert.equal(cfg.output.process, 'process');
  assert.equal(cfg.output.dbase, 'influxdb');
});

test('nodeClass config mapping — accepts deprecated overfill UI fields', () => {
  const cfg = loadConfig({
    enableHighVolumeSafety: undefined,
    highVolumeSafetyThresholdPercent: undefined,
    enableOverfillProtection: false,
    overfillThresholdPercent: 91,
  });

  assert.equal(cfg.safety.enableHighVolumeSafety, false);
  assert.equal(cfg.safety.highVolumeSafetyThresholdPercent, 91);
});
