CREATE TABLE IF NOT EXISTS vehicle_metric_definition (
  metric_key VARCHAR(64) NOT NULL PRIMARY KEY,
  label VARCHAR(80) NOT NULL,
  description VARCHAR(255) NOT NULL DEFAULT '',
  unit VARCHAR(24) NOT NULL DEFAULT '',
  category VARCHAR(40) NOT NULL,
  value_type VARCHAR(16) NOT NULL,
  protocols_json VARCHAR(512) NOT NULL,
  searchable TINYINT(1) NOT NULL DEFAULT 0,
  chartable TINYINT(1) NOT NULL DEFAULT 0,
  alertable TINYINT(1) NOT NULL DEFAULT 0,
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  sort_order INT NOT NULL DEFAULT 100,
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  KEY idx_metric_definition_capability (enabled, alertable, category, sort_order)
);

CREATE TABLE IF NOT EXISTS vehicle_metric_protocol_mapping (
  metric_key VARCHAR(64) NOT NULL,
  protocol VARCHAR(32) NOT NULL,
  source_field VARCHAR(160) NOT NULL,
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (metric_key, protocol),
  CONSTRAINT fk_metric_mapping_definition FOREIGN KEY (metric_key) REFERENCES vehicle_metric_definition(metric_key)
);

INSERT IGNORE INTO vehicle_metric_definition
(metric_key,label,description,unit,category,value_type,protocols_json,searchable,chartable,alertable,enabled,sort_order) VALUES
('speed_kmh','速度','车辆最新行驶速度','km/h','driving','numeric','["GB32960","JT808","YUTONG_MQTT"]',1,1,1,1,10),
('soc_percent','SOC','动力电池剩余电量百分比','%','energy','numeric','["GB32960","YUTONG_MQTT"]',1,1,1,1,20),
('alarm_active','协议告警位','协议报文是否携带活动告警','','safety','boolean','["GB32960","JT808","YUTONG_MQTT"]',1,0,1,1,30),
('freshness_sec','离线时长','最新数据距当前时间的秒数','s','quality','numeric','["GB32960","JT808","YUTONG_MQTT"]',1,1,1,1,40),
('data_delay_sec','数据延迟','服务接收时间与设备事件时间的差值','s','quality','numeric','["GB32960","JT808","YUTONG_MQTT"]',1,1,1,1,50),
('total_mileage_km','总里程','车辆累计行驶里程','km','driving','numeric','["GB32960","JT808","YUTONG_MQTT"]',1,1,0,1,60),
('longitude','经度','WGS84/协议归一化经度','°','location','numeric','["GB32960","JT808","YUTONG_MQTT"]',1,0,0,1,70),
('latitude','纬度','WGS84/协议归一化纬度','°','location','numeric','["GB32960","JT808","YUTONG_MQTT"]',1,0,0,1,80);

INSERT IGNORE INTO vehicle_metric_protocol_mapping(metric_key,protocol,source_field) VALUES
('speed_kmh','GB32960','gb32960.vehicle.speed_kmh'),
('speed_kmh','JT808','jt808.location.speed_kmh'),
('speed_kmh','YUTONG_MQTT','yutong.vehicle.speed_kmh'),
('soc_percent','GB32960','gb32960.vehicle.soc_percent'),
('soc_percent','YUTONG_MQTT','yutong.vehicle.soc_percent'),
('alarm_active','GB32960','gb32960.vehicle.alarm_flag'),
('alarm_active','JT808','jt808.location.alarm_flag'),
('alarm_active','YUTONG_MQTT','yutong.vehicle.alarm_flag'),
('freshness_sec','PLATFORM','vehicle_realtime_location.updated_at'),
('data_delay_sec','PLATFORM','received_at-event_time'),
('total_mileage_km','GB32960','gb32960.vehicle.total_mileage_km'),
('total_mileage_km','JT808','jt808.location.total_mileage_km'),
('total_mileage_km','YUTONG_MQTT','yutong.vehicle.total_mileage_km'),
('longitude','PLATFORM','vehicle_realtime_location.longitude'),
('latitude','PLATFORM','vehicle_realtime_location.latitude');
