-- Align alertable metric mappings with the canonical fields emitted by each
-- production protocol parser. This is forward-only because migration 005 may
-- already be journaled on an ECS.
UPDATE vehicle_metric_protocol_mapping
SET source_field='yutong_mqtt.data.meter_speed'
WHERE metric_key='speed_kmh' AND protocol='YUTONG_MQTT';

UPDATE vehicle_metric_protocol_mapping
SET source_field='yutong_mqtt.data.battery_capacity_soc'
WHERE metric_key='soc_percent' AND protocol='YUTONG_MQTT';

UPDATE vehicle_metric_protocol_mapping
SET source_field='gb32960.alarm.general_alarm_flag'
WHERE metric_key='alarm_active' AND protocol='GB32960';

DELETE FROM vehicle_metric_protocol_mapping
WHERE metric_key='alarm_active' AND protocol='YUTONG_MQTT';

UPDATE vehicle_metric_definition
SET protocols_json='["GB32960","JT808"]'
WHERE metric_key='alarm_active';

INSERT INTO vehicle_metric_protocol_mapping(metric_key,protocol,source_field) VALUES
('longitude','GB32960','gb32960.position.longitude'),
('longitude','JT808','jt808.location.longitude'),
('longitude','YUTONG_MQTT','yutong_mqtt.data.longitude'),
('latitude','GB32960','gb32960.position.latitude'),
('latitude','JT808','jt808.location.latitude'),
('latitude','YUTONG_MQTT','yutong_mqtt.data.latitude')
ON DUPLICATE KEY UPDATE source_field=VALUES(source_field);
