Modelling atmospheric escape and Mg ii near-ultraviolet absorption of the highly irradiated hot Jupiter WASP-12b