-- Durable Kafka checkpoint for the event-time alert stream shadow/active worker.
-- Business alert effects and next_offset will share one MySQL transaction when
-- active evaluation is enabled. Kafka group offsets are committed afterwards.
CREATE TABLE IF NOT EXISTS vehicle_alert_stream_checkpoint (
  consumer_group VARCHAR(128) NOT NULL,
  topic VARCHAR(255) NOT NULL,
  partition_id INT NOT NULL,
  next_offset BIGINT NOT NULL,
  high_watermark BIGINT NOT NULL DEFAULT 0,
  processed_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
  valid_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
  invalid_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
  late_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
  replay_skipped_count BIGINT UNSIGNED NOT NULL DEFAULT 0,
  last_event_at DATETIME(3) NULL,
  last_received_at DATETIME(3) NULL,
  last_source_event_id VARCHAR(128) NOT NULL DEFAULT '',
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (consumer_group, topic, partition_id),
  KEY idx_alert_stream_updated (updated_at),
  KEY idx_alert_stream_lag (consumer_group, high_watermark, next_offset)
);
