When they arrived in Pakistan on Tuesday for a five-day tour, the Duke and Duchess of Cambridge entered a country in which many foreign sports teams still refuse to play and few tourists dare put on their itinerary. The Foreign Office, while acknowledging things have improved in Pakistan, still advises against all travel to some parts of the country, including the southern city of Peshawar. Pakistan’s image in the West as a lawless place in which no westerner is safe was illustrated by President Trump’s infamous tweet in early 2018, in which he described the country as an unreliable ally that has 'given the United States nothing but lies and deceit' when it comes to the Global War on Terror.

But that is not the country I recognised when I recently visited, after a ten year hiatus. What I saw was a nation which has transformed its internal security. Kensington Palace is right to describe the Cambridges’ visit as 'showcasing Pakistan as it is today'. It is a timely and healthy corrective to common perceptions.

When I used to go to Pakistan regularly in 2009 and 2010 as the Pakistan policy lead for the MoD, the country was facing an existential crisis. It received 2.7 million refugees from Afghanistan between the Soviet invasion of 1979 and allied invasion of 2001. These refugees had settled initially amongst their fellow Pashtuns in the Federally Administered Tribal Areas (FATA), a breeding ground for the jihadis who fought the Russians and then the US-led coalition. By 2008, many of these jihadis had moved out of the FATA, an area not under government control, into the Swat Valley. There, not far from the capital Islamabad, they posed a direct threat to the authority of the central government.

The Pakistani military was in a quandary about how to respond. In general, the Pakistani population was ambivalent about their own military. Conditioned by tribal customs, they preferred to support their fellow Pashtun Muslims against the infidels of the army. By 2009, the reputation of the army in the eyes of its own population was so tainted that soldiers’ funerals had to be conducted in secret. The problem for the government was how to support George W Bush’s global war on terror while still abiding by ancient tribal customs of hospitality to their fellow Muslims.

Yet the Swat valley insurgency proved to be the high water mark for the jihadis. Their brutal behaviour and imposition of strict sharia law in the region – known for its beauty and famed for its wedding venues – shocked the population into supporting the government fight-back. In 2009, the army succeeded in re-taking the Swat region.

The real psychological turning point came when the group Tehrik-e Taliban Pakistan (TTP) attacked the Army Public School in Peshawar in 2014, which killed more than 140 people, mainly children. The terrorists were found to have all been foreigners. From that moment on, popular attitudes shifted; the Pashtun culture of hospitality was broken and the army and government were now supported by the population to launch a counter-insurgency operation.

As has been discovered in both Iraq and Afghanistan, winning the military battles is the easy part; it is winning the peace that is harder. The Pakistanis were quick to recognise that this was more than a military battle and, in 2015, issued a National Action Plan – focused on assimilating the FATA. The government has taken control of 28,000 madrassas, their teachers and the curriculum, with local mullahs’ consent. The country's border with Afghanistan is being sealed off; I visited the Khyber Pass where a new 833km fence is being built, with 700 guard forts, and which will soon be augmented by CCTV and sensor devices. This $1bn effort has choked off the Haqqani and TTP terrorist networks. All those on the US ‘watch-list’ in Pakistan have been arrested.

Much credit for this must go to the Chief of the Army Staff, General Bajwa. He was picked over more senior officers on account of his strong pro-democracy attitudes. He brings to the role an unusually beneficent attitude to India, having served as chief of staff to an Indian three star on a UN mission in Congo. He has since proved an able interlocutor in trying to de-escalate tensions in Yemen and Afghanistan.

Whilst his predecessors did much of the military graft, he has focused on improving society through soft power. General Bajwa told me that 'a strong army needs a sovereign economy' and he shares with his PM a desire for Pakistan to attract investment, not aid. To this end Pakistan is now diversifying its sources of supply and producing more of its own goods. His character and broad vision for the overall strength of Pakistan explains why PM Imran Khan has recently extended his term for three years.

Khan, too, has shown strength and courage with his anti-corruption drive, his Peace and Prosperity agenda and his release of the Indian pilot shot down in the Pulwama incident in Kashmir this February. As Spinoza observed, 'Peace is not an absence of war, it is a virtue, a state of mind, a disposition for benevolence, confidence, justice'. Khan and General Bajwa seem to have that virtue.

But the effort has cost Pakistan. The fence on the Afghan border alone cost over $1bn. The absorption of the FATA is another continuing cost. Imran Khan’s cancellation of inherited corrupt contracts has cost the government billions. Now is a time when Pakistan needs and deserves our support. So the royal visit is well timed and will hopefully reinforce existing bonds and secure them for the future.

In 2001, George W Bush declared 'Every nation in every region now has a decision to make: either you are with us or you are with the terrorists.' Pakistan has made huge efforts at great cost over the past decade to tackle the terror threat. Now is not the time to ask for more from the country; now is the time for the world to give it more respect.

Maj Gen Jonathan Shaw was the two star International Security Policy in the MoD 2009-2011. He is now chairman of Optima Group.