import type { ParamFieldDef } from '@/types/workflow'

/** 协议类型（与平台 DeviceProtocol 对齐，MVP 先开放 Modbus） */
export const PROTOCOL_TYPE_OPTIONS = [
  { label: 'Modbus TCP', value: 'modbus-tcp' },
  { label: 'Modbus RTU', value: 'modbus-rtu' }
]

export const DEVICE_IO_MODE_OPTIONS = [
  { label: '读信号（接收）', value: 'read' },
  { label: '写信号（输出）', value: 'write' }
]

export const MODBUS_DATA_TYPE_OPTIONS = [
  { label: '布尔', value: 'bool' },
  { label: 'Int16', value: 'int16' },
  { label: 'UInt16', value: 'uint16' },
  { label: 'Int32', value: 'int32' },
  { label: 'Float32', value: 'float32' }
]

export const MODBUS_COMPARE_OPTIONS = [
  { label: '等于', value: 'eq' },
  { label: '不等于', value: 'ne' },
  { label: '大于', value: 'gt' },
  { label: '大于等于', value: 'gte' },
  { label: '小于', value: 'lt' },
  { label: '小于等于', value: 'lte' },
  { label: '值发生变化', value: 'changed' }
]

export const MODBUS_WRITE_MODE_OPTIONS = [
  { label: '持续写入', value: 'hold' },
  { label: '脉冲 (写入后复位)', value: 'pulse' }
]

const READ = { key: 'ioMode', equals: 'read' as const }
const WRITE = { key: 'ioMode', equals: 'write' as const }

/** 设备控制：Modbus 读写合一，通过 ioMode 切换读/写配置 */
export function deviceControlParams(): ParamFieldDef[] {
  return [
    {
      key: 'ioMode',
      label: '读写模式',
      type: 'select',
      required: true,
      defaultValue: 'read',
      options: DEVICE_IO_MODE_OPTIONS
    },
    {
      key: 'protocol',
      label: '协议类型',
      type: 'select',
      required: true,
      defaultValue: 'modbus-tcp',
      options: PROTOCOL_TYPE_OPTIONS
    },
    {
      key: 'host',
      label: '连接地址',
      type: 'string',
      required: true,
      placeholder: 'modbus-tcp: 10.0.2.30:502；modbus-rtu: COM3,9600,8,N,1'
    },
    {
      key: 'address',
      label: '寄存器地址',
      type: 'number',
      required: true,
      defaultValue: 0,
      placeholder: '如 40001 或 0（视驱动约定）'
    },
    {
      key: 'dataType',
      label: '数据类型',
      type: 'select',
      required: true,
      defaultValue: 'bool',
      options: MODBUS_DATA_TYPE_OPTIONS
    },
    // ── 读模式 ──
    {
      key: 'compareOp',
      label: '触发条件',
      type: 'select',
      required: true,
      defaultValue: 'eq',
      options: MODBUS_COMPARE_OPTIONS,
      when: READ
    },
    {
      key: 'pollIntervalMs',
      label: '轮询间隔 (ms)',
      type: 'number',
      defaultValue: 500,
      when: READ
    },
    // ── 写模式 ──
    {
      key: 'writeValue',
      label: '写入值',
      type: 'string',
      required: true,
      placeholder: '如 1 / true / 100',
      when: WRITE
    },
    {
      key: 'writeMode',
      label: '写入模式',
      type: 'select',
      defaultValue: 'hold',
      options: MODBUS_WRITE_MODE_OPTIONS,
      when: WRITE
    },
    {
      key: 'pulseMs',
      label: '脉冲宽度 (ms)',
      type: 'number',
      defaultValue: 500,
      placeholder: 'writeMode=pulse 时有效',
      when: WRITE
    },
    {
      key: 'verifyRead',
      label: '写入后回读校验',
      type: 'boolean',
      defaultValue: true,
      when: WRITE
    }
  ]
}
