CREATE TABLE IF NOT EXISTS sms_auth_events (
  id TEXT PRIMARY KEY,
  phone_number TEXT NOT NULL,
  scene TEXT NOT NULL,
  action TEXT NOT NULL,
  success BOOLEAN NOT NULL,
  ip TEXT,
  user_agent TEXT,
  provider TEXT,
  provider_request_id TEXT,
  provider_biz_id TEXT,
  provider_out_id TEXT,
  delivery_status TEXT NOT NULL DEFAULT 'pending',
  delivery_report_raw_json JSONB,
  delivery_reported_at TEXT,
  created_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS sms_auth_events_phone_created_idx
  ON sms_auth_events (phone_number, created_at DESC);

CREATE INDEX IF NOT EXISTS sms_auth_events_ip_created_idx
  ON sms_auth_events (ip, created_at DESC);

CREATE INDEX IF NOT EXISTS sms_auth_events_provider_biz_id_idx
  ON sms_auth_events (provider_biz_id);

CREATE INDEX IF NOT EXISTS sms_auth_events_provider_out_id_idx
  ON sms_auth_events (provider_out_id);
