In this surveillance image taken on Jan. 6, 2017, U.S. citizen Zia Zafar is seen before shooting Christopher Ashcraft, a U.S. Consulate official, in Guadalajara, Mexico. (Justice Department/AP)

As he drove out of his gym’s parking garage and saw a man standing at the top of the ramp, Christopher Ashcraft cursed to himself.

He knew something was very wrong even before the bullet hit his chest.

The U.S. Consulate official in Guadalajara, Mexico, put pressure on the wound and called his father to say goodbye. But Ashcraft narrowly survived the Jan. 6, 2017 shooting. This week, Zia Zafar, 33, was sentenced to 22 years in prison after pleading guilty to attempted murder of a government official and related charges.

Although Ashcraft made a full recovery, the bullet cannot be removed from his spine and could cause damage if it is dislodged by a future injury, prosecutors said. He can also no longer undergo an MRI scan.

Zafar, of Chino Hills, Calif., wanted to kill a representative of the United States and thought he would be able to get away with the crime in Mexico because “it’s a third world country,” according to excerpts of his conversations with a psychiatrist included in court papers. He chose Ashcraft because the consulate official was “available . . . and vulnerable.”

Zafar, a medical school graduate who was interning at a hospital in Guadalajara, maintained that his actions were the result of untreated mental illness, although he chose not to go to trial, arguing that he was not guilty by reason of insanity.

“I know that, despite my mental illness, that my actions were illegal that and I will do whatever is necessary to treat my mental illness,” he wrote in a letter to the court.

But prosecutors pointed to his careful planning and attempts to hide his crime as evidence Zafar knew what he was doing. The case was prosecuted in federal court in Alexandria.

“Simply having a mental health diagnosis does not, and should not, excuse or mitigate a violent, premeditated act that the defendant clearly knew was wrong,” prosecutors Jamie Perry and Ronald L. Walutes Jr. wrote in sentencing papers.

Zafar owned several firearms but bought a new one from a dealer in Mexico to shoot Ashcraft, according to court records. He took more than 100 photos of the U.S. Consulate in Guadalajara and of diplomats’ cars, including the one driven by Ashcraft. On the day of the shooting, he walked to an area of the ramp where he wouldn’t be seen by a security guard.

Ashcraft realized he was being followed when he left the gym that day, prosecutors said. He steered himself toward a group of people, and the man, wearing a wig and sunglasses, disappeared.

But when he got into his car and out of the parking garage, prosecutors say, Ashcraft saw the same man waiting for him with a gun.

Zafar was identified because he had stopped at Starbucks before the shooting and signed his name on a receipt. When interviewed by the FBI, he initially claimed that he had gone straight from the hospital to his home that day and that someone may have been impersonating him.

Zafar had attended medical school in Guadalajara, studying psychiatry. During medical school, he began abusing Adderall to study and then overdosing on Benadryl to sleep, causing seizures, according to court papers. He had a history of anxiety and attention-deficit hyperactivity disorder but not hallucinations. After his arrest, four doctors concluded that he probably has a form of schizophrenia, although prosecutors emphasized that two of the four thought he may have been faking some symptoms.

A public defender representing Zafar declined to comment on the sentence, which was imposed Wednesday.