Understanding the deposition of Mg and intercalation of Mg2+ for future Mg-ion batteries