CREATE TABLE IF NOT EXISTS sales_takeover_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  tenant_id BIGINT UNSIGNED NOT NULL,
  location_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  from_user_id BIGINT UNSIGNED NULL,
  from_user_name_snapshot VARCHAR(120) NULL,
  to_user_id BIGINT UNSIGNED NULL,
  to_user_name_snapshot VARCHAR(120) NULL,
  order_status_snapshot VARCHAR(40) NULL,
  takeover_at DATETIME NOT NULL,
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  KEY idx_sales_takeover_scope (tenant_id, location_id, takeover_at),
  KEY idx_sales_takeover_order (order_id),
  KEY idx_sales_takeover_from_user (from_user_id),
  KEY idx_sales_takeover_to_user (to_user_id),
  CONSTRAINT fk_sales_takeover_order FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_sales_takeover_from_user FOREIGN KEY (from_user_id) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT fk_sales_takeover_to_user FOREIGN KEY (to_user_id) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
