In a visit with a friend in 2012, I had a long chat with a young active duty marine who her younger sister had recently eloped with. The dude was kind of a bum and a weirdo for sure, but what he said about his tours in Iraq and Afghanistan have since been prophetic to me. He swore that the Iraqis would fight alongside the Americans when they came under attack, while the Afghans would drop their weapons and retreat when things got too hot. Fast forward almost a decade and the Iraqis have since driven ISIS out of their country, admittedly under cover of US airstrikes and special forces, but the Afghan army is now literally melting away despite there still being a few thousand US troops there and the same degree of air support.
The fact is that the 20 years of nation building we've payed billions for haven't convinced Afghans to risk their skin to protect it. A soldier deserting their post in the middle of the night doesn't feel compelled to fight for the generation of girls who have been able to go to school, to speak nothing of their government. There isn't much inspiration in defending what an occupying force has constructed. Let's look at morale. While one side is deserting, the other is sending suicide bombers.
https://www.politico.com/news/magazine/2021/07/06/afghanistan-war-malkasian-book-excerpt-497843