/** 物联网监测模块 — 初始种子数据（localStorage 持久化） */

export const SENSOR_TYPES = [
  { value: 'strain', label: '应变计' },
  { value: 'vibration', label: '振动传感器' },
  { value: 'tilt', label: '倾角仪' },
  { value: 'temperature', label: '温度传感器' },
  { value: 'humidity', label: '湿度传感器' },
]

export const SENSOR_STATUS = {
  ONLINE: 'online',
  OFFLINE: 'offline',
  MAINTENANCE: 'maintenance',
}

export const COLLECTION_STATUS = {
  IDLE: 'idle',
  RUNNING: 'running',
  PAUSED: 'paused',
  ERROR: 'error',
}

export const PROCESSING_STATUS = {
  PENDING: 'pending',
  RUNNING: 'running',
  COMPLETED: 'completed',
  FAILED: 'failed',
}

export const ALERT_LEVEL = {
  INFO: 'info',
  WARNING: 'warning',
  CRITICAL: 'critical',
}

export const ALERT_STATUS = {
  OPEN: 'open',
  ACKNOWLEDGED: 'acknowledged',
  RESOLVED: 'resolved',
}

const now = () => new Date().toISOString()

export function createInitialIotState() {
  const t = now()
  return {
    sensors: [
      {
        sensor_id: 1,
        sensor_code: 'STR-ND-01',
        name: '南塔应变计-01',
        bridge_section: '南塔主梁',
        sensor_type: 'strain',
        status: SENSOR_STATUS.ONLINE,
        install_location: '南塔 L2 腹板',
        sampling_hz: 50,
        threshold_max: 1200,
        threshold_min: -50,
        last_heartbeat: t,
        firmware_version: 'v2.3.1',
        remark: '主监测点',
        created_at: t,
        updated_at: t,
      },
      {
        sensor_id: 2,
        sensor_code: 'VIB-SP-02',
        name: '跨中振动-02',
        bridge_section: '跨中箱梁',
        sensor_type: 'vibration',
        status: SENSOR_STATUS.ONLINE,
        install_location: '跨中底板',
        sampling_hz: 200,
        threshold_max: 8.5,
        threshold_min: 0,
        last_heartbeat: t,
        firmware_version: 'v1.8.0',
        remark: '',
        created_at: t,
        updated_at: t,
      },
      {
        sensor_id: 3,
        sensor_code: 'TIL-NP-03',
        name: '北塔倾角-03',
        bridge_section: '北塔墩台',
        sensor_type: 'tilt',
        status: SENSOR_STATUS.OFFLINE,
        install_location: '北塔承台',
        sampling_hz: 10,
        threshold_max: 0.35,
        threshold_min: -0.35,
        last_heartbeat: '2026-05-30T08:12:00.000Z',
        firmware_version: 'v2.1.4',
        remark: '待现场检修',
        created_at: t,
        updated_at: t,
      },
      {
        sensor_id: 4,
        sensor_code: 'TMP-AB-04',
        name: '环境温度-04',
        bridge_section: '全桥环境监测',
        sensor_type: 'temperature',
        status: SENSOR_STATUS.ONLINE,
        install_location: '桥面气象站',
        sampling_hz: 1,
        threshold_max: 45,
        threshold_min: -10,
        last_heartbeat: t,
        firmware_version: 'v3.0.2',
        remark: '',
        created_at: t,
        updated_at: t,
      },
    ],
    collectionTasks: [
      {
        task_id: 1,
        task_name: '南塔应变连续采集',
        sensor_id: 1,
        interval_sec: 60,
        batch_size: 100,
        status: COLLECTION_STATUS.RUNNING,
        last_run_at: t,
        sample_count: 18420,
        error_message: '',
        created_at: t,
        updated_at: t,
      },
      {
        task_id: 2,
        task_name: '跨中振动高频采集',
        sensor_id: 2,
        interval_sec: 30,
        batch_size: 500,
        status: COLLECTION_STATUS.PAUSED,
        last_run_at: '2026-06-01T06:00:00.000Z',
        sample_count: 9620,
        error_message: '',
        created_at: t,
        updated_at: t,
      },
      {
        task_id: 3,
        task_name: '北塔倾角巡检采集',
        sensor_id: 3,
        interval_sec: 300,
        batch_size: 50,
        status: COLLECTION_STATUS.ERROR,
        last_run_at: '2026-05-30T08:15:00.000Z',
        sample_count: 2100,
        error_message: '传感器离线，连续 3 次心跳超时',
        created_at: t,
        updated_at: t,
      },
    ],
    processingJobs: [
      {
        job_id: 1,
        job_name: '应变去噪与特征提取',
        source_task_id: 1,
        pipeline: 'denoise → resample → feature_extract',
        status: PROCESSING_STATUS.COMPLETED,
        progress: 100,
        input_records: 12000,
        output_records: 11840,
        started_at: '2026-06-01T02:00:00.000Z',
        finished_at: '2026-06-01T02:18:00.000Z',
        created_at: t,
        updated_at: t,
      },
      {
        job_id: 2,
        job_name: '振动频谱分析',
        source_task_id: 2,
        pipeline: 'fft → band_energy → anomaly_score',
        status: PROCESSING_STATUS.RUNNING,
        progress: 62,
        input_records: 8000,
        output_records: 4960,
        started_at: '2026-06-01T08:00:00.000Z',
        finished_at: null,
        created_at: t,
        updated_at: t,
      },
    ],
    alerts: [
      {
        alert_id: 1,
        sensor_id: 3,
        level: ALERT_LEVEL.CRITICAL,
        title: '传感器离线',
        message: '北塔倾角-03 超过 15 分钟未上报心跳，采集任务已自动暂停。',
        status: ALERT_STATUS.OPEN,
        trigger_value: null,
        threshold_value: null,
        created_at: '2026-05-30T08:20:00.000Z',
        acknowledged_at: null,
        resolved_at: null,
      },
      {
        alert_id: 2,
        sensor_id: 1,
        level: ALERT_LEVEL.WARNING,
        title: '应变接近阈值',
        message: '南塔应变计-01 最新峰值 1086 με，超过预警线 90%。',
        status: ALERT_STATUS.ACKNOWLEDGED,
        trigger_value: 1086,
        threshold_value: 1200,
        created_at: '2026-06-01T07:30:00.000Z',
        acknowledged_at: '2026-06-01T08:00:00.000Z',
        resolved_at: null,
      },
      {
        alert_id: 3,
        sensor_id: 4,
        level: ALERT_LEVEL.INFO,
        title: '温度波动提醒',
        message: '桥面温度 1 小时内变化 6.2℃，已记录至环境日志。',
        status: ALERT_STATUS.RESOLVED,
        trigger_value: 6.2,
        threshold_value: 5,
        created_at: '2026-05-31T14:00:00.000Z',
        acknowledged_at: '2026-05-31T14:10:00.000Z',
        resolved_at: '2026-05-31T16:00:00.000Z',
      },
    ],
    nextIds: { sensor: 5, task: 4, job: 3, alert: 4 },
  }
}
