ALTER TABLE vehicle_alert_rule
  ADD COLUMN notification_targets_json TEXT NULL AFTER notification_channels_json;

UPDATE vehicle_alert_rule
SET notification_targets_json = CASE
  WHEN JSON_CONTAINS(notification_channels_json, '"in_app"') THEN
    '[{"channel":"in_app","recipientId":"platform-operators","label":"平台值班组"}]'
  ELSE '[]'
END
WHERE notification_targets_json IS NULL;

ALTER TABLE vehicle_alert_rule
  MODIFY COLUMN notification_targets_json TEXT NOT NULL;

ALTER TABLE vehicle_alert_notification
  ADD COLUMN recipient_ref VARCHAR(96) NOT NULL DEFAULT '' AFTER recipient,
  ADD COLUMN lease_token VARCHAR(96) NOT NULL DEFAULT '',
  ADD COLUMN lease_expires_at DATETIME(3) NULL,
  ADD KEY idx_alert_notification_dispatch (channel, delivery_status, lease_expires_at, id);
