-- Forward-only V2 access threshold schema. Safe to run repeatedly before API switch.
CREATE TABLE IF NOT EXISTS vehicle_access_threshold_config (
  id TINYINT NOT NULL PRIMARY KEY,
  version INT NOT NULL,
  default_threshold_sec INT NOT NULL,
  delay_threshold_sec INT NOT NULL,
  long_offline_sec INT NOT NULL,
  protocol_overrides_json LONGTEXT NOT NULL,
  updated_by VARCHAR(128) NOT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS vehicle_access_threshold_audit (
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  version INT NOT NULL,
  actor VARCHAR(128) NOT NULL,
  summary VARCHAR(255) NOT NULL,
  config_json LONGTEXT NOT NULL,
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_access_threshold_audit_version (version),
  KEY idx_access_threshold_audit_changed (changed_at)
);

INSERT IGNORE INTO vehicle_access_threshold_config (
  id,
  version,
  default_threshold_sec,
  delay_threshold_sec,
  long_offline_sec,
  protocol_overrides_json,
  updated_by
) VALUES (
  1,
  1,
  300,
  30,
  1800,
  '[{"protocol":"GB32960","thresholdSec":300},{"protocol":"JT808","thresholdSec":300},{"protocol":"YUTONG_MQTT","thresholdSec":600}]',
  'migration'
);
