import test from 'node:test'
import assert from 'node:assert/strict'
import {
  buildThresholdQuery,
  buildThresholdSavePayload,
  buildThresholdUpdatePayload
} from '../src/utils/waterSupplyUiModel.js'

test('builds the flat batch-save contract expected by the threshold API', () => {
  const payload = buildThresholdSavePayload({
    id: 'draft-id',
    deviceCodes: [' WS-FLOW-1 ', '', 'WS-FLOW-2', 'WS-FLOW-1'],
    warningType: ' 流量预警 ',
    warningCode: ' WARN-FLOW ',
    minValue: '20',
    maxValue: '40',
    periodStart: '08:00',
    periodEnd: '20:00',
    remark: ' 高峰期阈值 '
  })

  assert.deepEqual(payload, {
    deviceCodes: ['WS-FLOW-1', 'WS-FLOW-2'],
    warningType: '流量预警',
    warningCode: 'WARN-FLOW',
    minValue: 20,
    maxValue: 40,
    periodStart: '08:00',
    periodEnd: '20:00',
    remark: '高峰期阈值'
  })
})

test('builds an update payload that preserves the threshold id', () => {
  const payload = buildThresholdUpdatePayload({
    id: 'threshold-id',
    deviceCode: 'WS-FLOW-1',
    warningType: '流量预警',
    warningCode: 'WARN-FLOW',
    minValue: 20,
    maxValue: 40,
    periodStart: '08:00',
    periodEnd: '20:00',
    createTime: '2026-09-02 10:00:00'
  })

  assert.deepEqual(payload, {
    id: 'threshold-id',
    deviceCode: 'WS-FLOW-1',
    warningType: '流量预警',
    warningCode: 'WARN-FLOW',
    minValue: 20,
    maxValue: 40,
    periodStart: '08:00',
    periodEnd: '20:00'
  })
})

test('builds threshold list filters without alarm-only fields', () => {
  const query = buildThresholdQuery({
    pageNum: 2,
    pageSize: 20,
    deviceCode: 'WS-FLOW-1',
    warningType: '流量预警',
    keyword: ' ',
    alarmLevel: 2,
    alarmStatus: 0
  })

  assert.deepEqual(query, {
    pageNum: 2,
    pageSize: 20,
    deviceCode: 'WS-FLOW-1',
    warningType: '流量预警'
  })
})
