Prime Minister Stephen Harper hailed the end of Canada’s military mission in Afghanistan Wednesday as a “significant milestone in the fight against global terror.”

After 12 years, the loss of 162 lives and endless debate at home, the military mission formally ended with a ceremony in Kabul.

Canadian and international dignitaries looked on as the army took down the Canadian flag at NATO headquarters, still under heavy guard in the war-ravaged country.

In a statement, Harper praised the work of the more than 40,000 Canadian Forces members who “have fought to defeat the threat of terrorism, and to ensure the freedom of others to build a stronger, safer world. In the course of this fight, many have paid the ultimate price.

“Their courage and dedication has brought much pride to our country. I look forward to personally welcoming home the last contingent of Canada’s brave men and women of the Canadian Armed Forces when they return home on the final flight.”

The withdrawal of the last 100 Canadian soldiers, who stayed behind to train Afghan National Security Forces, will be completed over the next few days. The last troops will arrive home on March 18.

“Your strength has protected the weak; your bravery has brought hope to hopeless; and the helping hand you have extended to the Afghan people has given them faith that a better future is within their grasp," Deborah Lyons, the Canadian ambassador to Afghanistan, told the soldiers in Kabul Wednesday.

In a statement, Liberal defence critic Joyce Murray said “all Canadians join in extending our appreciation and thanks” to those who served in Afghanistan.

“All those who served have made enormous sacrifices during Canada's largest military deployment since the Second World War,” Murray said.

“Canadian women and men carried out their duties with the utmost professionalism and distinction. We are immensely proud and grateful of their service to enhance peace, stability, and hope in a troubled region of the world.”

Canada must be sure to “take the best possible care” of the veterans who are returning or who have already returned from Afghanistan, Murray said.

Today, Canadians remain divided on the success of the Afghan mission. Heavy combat, roadside bombings and other incidents took the lives of 158 Canadian soldiers, one diplomat, one journalist and two civilian contractors. Many soldiers who returned home injured or profoundly changed by the experience are now struggling with post-traumatic stress disorder, inadequate mental health care services, unemployment and other issues.

“This country was put to the test,” retired Col. Pat Stogran, who served as commander in Afghanistan, told CTV’s Canada AM Wednesday. “The jury is still out right now.”

Since Canadian troops first arrived in October 2002, there have been notable improvements in Afghanistan: access to education, especially for girls, a drop in infant and maternal mortality rates, and infrastructure projects, among others.

From 2001 to 2013, Canada spent $1.65 billion on reconstruction and development in Afghanistan and made efforts to improve women’s rights and the living conditions of rural Afghans.

But security is deteriorating in many parts of the country, raising serious questions about Afghanistan’s future. Just this week, a British-Swedish journalist was gunned down in a heavily policed part of Kabul. Feday-e-Mahaz, a Taliban splinter group, claimed responsibility for the killing.

Access to health care is another big problem. Diseases such as polio, malaria, measles and tuberculosis are a huge burden on the underfunded health care system.

Stogran, an outspoken former veterans’ affairs ombudsman, said he left the army “discouraged” by what he called a “falling apart” of the Canadian government’s approach to the Afghan mission. But he remains “very proud” of the soldiers’ accomplishments and sacrifices.

“I’m amazed at how our young people rallied to the call and the job our folks did overseas,” he said. Not only did Canadian soldiers risk their lives every day, they also collected money for aid projects and helped build schools and wells, Stogran said.

The removal of the Canadian flag in Kabul was “a very solemn occasion for me,” he said. But ongoing conflicts in the Middle East mean that “we’re not finished this yet,” he added.

“We are at a point now where our nation has to heal and we have to celebrate the accomplishments of our young people. They’ve planted a seed in Afghanistan that we should all hope and pray will serve the long-term benefit of a very troubled nation,” Stogran said.

“But we can’t dust ourselves off and say: ‘Well, I’m glad that Afghanistan is over with.’”

NATO’s combat forces are also scheduled to leave Afghanistan at the end of the year, but member nations have promised $4.1 billion a year in funding for the country’s security forces.

Lyons, the Canadian ambassador, said Wednesday that Canada will remain involved in building Afghanistan’s economy, particularly the resource sector.

In his statement, the prime minister reiterated that Canada “will continue to play an important role in supporting efforts that contribute to building a better future for all Afghans.”

Harper also said that when he greets the last soldiers as they return home, he will announce plans to “formally commemorate the mission in Afghanistan.”

With files from The Canadian Press