On 23 June, 12 boys went exploring in Thailand's Chiang Rai province with their football coach - and ended up trapped deep inside a cave underneath a mountain.

What happened over the following two weeks has gripped the world. It is a remarkable story of friendship, human endurance - and the lengths some people will go to save someone else's child.

This is the story of the Wild Boars.

The birthday party that went wrong

It all began with a birthday.

On Saturday 23 June, Peerapat "Night" Sompiangjai turned 17 - a milestone most young people around the world would want to celebrate in style.

His family had prepared a bright yellow SpongeBob SquarePants birthday cake and several colourfully wrapped presents at their home in a rural village in Mae Sai district.

But Night wasn't rushing home that day. He was out with his friends, the other members of local youth football team the Wild Boars, and their assistant coach, Ekkapol "Ake" Chantawong.

When their football practice ended, they raced through the rice paddies on their bicycles and up into the forested hills that lately had been blanketed in rain.

Their destination: the Tham Luang cave, a favourite haunt for the boys, who loved exploring the nooks and crannies of the mountain range towering over Mae Sai.

Once at the mouth of Tham Luang, they stashed their bikes and bags by the cave entrance.

The team and their young coach were ready to celebrate Night's birthday. They had often ventured deep into Tham Luang, sometimes as far as 8km, for initiation rites where they would write the names of new team members on a cave wall.

In high spirits, they clambered into the cave with just their torches. They didn't need much else - after all, they were only planning to be there for an hour.

They would not emerge until two weeks later.

Back at Night's home, his family began to worry. His birthday cake sat untouched.

Where were the Wild Boars?

Snaking for 10km beneath the cloud-swathed mountain range that separates Thailand and Myanmar is Tham Luang, the fourth biggest Thai cave system.

Named after a mountain shaped like a reclining woman, its full name is Tham Luang Khun Nam Nang Non - "the great cave and water source of the sleeping lady mountain". Rich in folklore, it is a popular destination for day-trippers - and adventurous children.

It has its dangers - people have gone missing in Tham Luang before. And once monsoon season starts in July, the cave goes from innocuous to extremely dangerous.

The cave can flood up to 5m (16ft) during the rainy season, and should only be entered between November and April.

"The water is moving, it's muddy and there is almost no visibility," local guide Joshua Morris told the BBC.

And once the cave floods - it's risky even for experienced divers.

Almost everyone in Mae Sai knows this. So when the parents of the Wild Boars began to worry about their missing boys, they headed straight to the cave. The boys' plans to visit Tham Luang had been discussed in a group chat on a messaging app with other friends.

They found the bikes, the bags, and some football shoes outside. They raised the alarm.

Deep in the cave, the Wild Boars found themselves in trouble. It had been raining for the last few days, and all that water falling on the mountain had to go somewhere.

That somewhere was the Tham Luang cave system, which was fast filling up.

One initial account from the boys suggests they were caught off-guard by a flash flood. They needed to get out, but instead had no choice but to scramble even deeper into the cave.

The Wild Boars eventually found themselves marooned on a small rocky shelf about 4km from the cave entrance, past a normally dry point known as Pattaya Beach which by now was flooded.

Swallowed up by an unforgiving mountain and surrounded by darkness, the boys and the coach lost all sense of time. Fear, perhaps even terror, would no doubt have crept in.

But they were nothing but determined to survive. The group used rocks to dig 5m deeper into the shelf, to create a cavern where they could huddle together and keep warm.

Coach Ake, a former monk, taught the boys meditation techniques - to help them stay calm and use as little air as possible - and told them to lie still to conserve their strength.

But an extraordinary set of circumstances also worked in their favour.

They apparently had no food - but they did have a supply of drinkable water in the form of moisture dripping from the cave walls.

It was dark, but they had their torches. There was also enough air for a while - because the porous limestone and cracks in the rocks meant air could come through.

They had the right conditions to survive - at least for a little while. And most importantly, the Wild Boars had one another.

Now came the hardest bit - hoping for rescue.

Outside the cave, a full-blown rescue operation was quickly unfolding.

Authorities called in the elite Thai Navy Seals, the national police, and other rescue teams. Local volunteers also pitched in.

Initial investigations found footprints at one of the chambers in the cave - but no other sign the boys were still alive.

The Wild Boars were somewhere in the twisted depths of Tham Luang - but where exactly? And more importantly - how could rescuers get to them?

Exploring the cave was a challenge - most of the Navy divers had little cave diving experience. And the weather was merciless - heavy rainfall meant the water level was still rising, flooding chambers and cutting off rescuers from parts of the cave.

Engineers desperately tried to pump water out of the cave - but struggled, at least at first.

At the start, "no one really had any idea what to do", one volunteer said. Officials brought whatever equipment they could think of - small water pumps, long pipes, knives and shovels - but much of it was apparently unsuitable.

They even tried drilling into the mountainside, desperate to find cracks into the cave system which they could squeeze into, and used drones with thermal sensors to try to locate the boys.

Rescuers also turned to the villagers for local knowledge. The Thai Navy Seals found a boy, a Wild Boar member who happened to have skipped the cave expedition. He recalled a place in the complex they'd visited before - called Pattaya Beach.

Could the missing 13 be there?

Amid the flurry of rescue operations, a small group kept vigil at the mouth of the cave.

These were the boys' families, worriedly offering prayers for their lives. Among them was Tum Kantawong, the godmother of Coach Ake.

Every day she went up the mountain, carrying fruits, incense and candles. "It was to show respect to the spirit that protects the cave. I asked her to protect the 13 kids," she said.

The group gradually expanded to include concerned teachers from the schools the Wild Boars attended.

"We wanted to be the first to welcome the boys when they came out," said school administrator Ampin Saenta, who is so close to one boy, Adul, that she calls herself his "mama-teacher".

Classmates of the Wild Boars held group prayers, sang songs of encouragement into the cave, folded paper cranes, and posted messages of hope on school noticeboards.

Villages rallied together, donating money and hundreds of packages of food to the relatives of the boys and their coach.

That sense of community soon began to spread, as the story gained the nation's attention. Volunteers from other parts of Thailand flew in, while Thai social media lit up with expressions of love and support.

But it was about to get even bigger.

The first international rescuers arrived on Thursday 28 June.

These were US air force rescue specialists, and cave divers from the UK, Belgium, Australia, Scandinavia, and many other countries. Some had volunteered, and some were called in by Thai authorities.

Others were roped in when it became clear just how monumental the search effort would be.

Over the next few days, they and the Thai divers would fight a constant battle with the elements. They had to swim against a strong current, and were often forced back by rising floodwaters.

On Sunday 1 July - just over a week after the boys went missing - the rescuers made some progress. They reached a large cavern that would be later dubbed "chamber three" and serve as a key base for the divers.

It also happened to be the birthday of Note - one of the "Thai cave boys", as they were now dubbed by the media. All, however, were still lost to the world.

But not for long. The very next day, two British divers made an incredible discovery.

'Thirteen? Brilliant!'

John Volanthen and Rick Stanton had been braving Tham Luang's narrow, murky passageways for several days, laying out guide ropes and searching for signs of life.

On Monday, the two men finally reached Pattaya Beach. But there was nothing.

They continued onwards into the darkness. Then, a few hundred metres further, they found an air pocket.

"Wherever there is air space we surface, we shout, we smell," John told the BBC. It's a standard procedure for such rescue operations.

"We smelt the children before we saw or heard them."

Soon, the light from John's torch illuminated an electrifying sight - the boys emerged from the darkness, coming down the ledge towards him.

Rick started counting the boys, while John asked: "How many of you?"

"Thirteen!" came the reply in English.

"Thirteen? Brilliant!"

Next to John, Rick couldn't quite believe what he was seeing. "They're all alive!"

The lost Wild Boars had been found.

The two divers spent some time with the boys - trying to boost their morale. Then, they left lights with the boys, and promised to return later with food.

The extraordinary encounter was recorded on the divers' cameras - and swiftly posted online. The jubilation was instant, and worldwide.

Wracked with worry for much of the past week, the Wild Boars' parents were ecstatic to see their children had miraculously survived. They looked thin, but were otherwise in relatively good shape.

The boys and their coach were quickly joined by a military medic and Navy SEAL divers who would stay with them for the rest of the ordeal.

After nine days in the darkness, the Wild Boars once again saw light. They longed for proper food, and begged for pad krapao, a rice dish with meat stir fried with basil.

But doctor's orders were that they be put on a special diet of medicated liquid food, and mineral water with added vitamins.

A third boy, Dom, spent his birthday in the cave.

Rescuers set to work in figuring out how to extract 13 people - some of whom couldn't swim - from a winding, flooded 4km-long stretch of caves that even experienced divers would struggle with.

"Time is not on our side because they're expecting heavy rains within three days," diver Ben Reymenants told the BBC at the time.

"Now the real hard work comes."

A volunteer army

The astonishing discovery of the children deep in a mountain cave catapulted tiny Mae Sai into the international spotlight.

Overnight, journalists from all around the world descended on the district, as even more rescue volunteers from around the world poured in.

A small makeshift town mushroomed at the rural country park by the cave entrance.

Food stalls were set up - some staffed by members of the Thai royal kitchen - serving free drinks, hot noodles, chicken rice, and even ice lollies.

No job was too small to do.

The country park toilets were dirty and stretched beyond capacity - so people began cleaning them. Workers needed to get up and down the mountain - so drivers offered free lifts. Rescuers were covered in mud - so a local laundromat cleaned their clothes every night.

Spirits were high, but then a fatal accident devastated the community.

A hero dies

Former Navy Seal diver Saman Gunan was one of many volunteers who had rushed to help in the rescue.

On 6 July, while on a routine run to deliver air tanks to the boys, he lost consciousness after running out of air for himself. His dive buddy pulled him out and tried to revive him.

But Saman could not be saved. He was only 38 years old.

His funeral took place later that day. Buddhist monks murmured prayers for Saman as incense burned.

His wife, Waleeporn Gunan, said: "Saman once said we never know when we're going to die… so we need to cherish every day."

The death hit home the danger of the rescue mission, and the risks facing the boys. Saman was a fit and healthy diver who had also represented Thailand in triathlons.

Ratdao Chantapoon, the mother of cave boy Note, was said to have told a friend: "The Navy Seal had practised for so long, and was so strong, but also died. How about a boy who has never dived before?"

There was another thing to worry about too - despite efforts to replenish the air, oxygen levels in the chamber had fallen to 15%, lower than the usual 21%.

Time was running out.

Rescuers had identified three possible options:

Training the boys to dive through flooded areas of the cave - a process so ripe with potential for disaster it was widely considered a last resort

Pumping water from the cave and waiting for water levels to recede naturally - but this could take up to four months

Finding or drilling alternative passages into the cave

The divers started practising with some local boys at a swimming pool - figuring out how to transport a child safely underwater.

Other solutions, such as an offer of a kid-sized submarine designed by tech entrepreneur Elon Musk's engineers, would be rejected as unsuitable.

The rescue team faced conditions so difficult that even simple tasks - setting up air and phone lines in the cave - seemed impossible at first because of the labyrinthine layout of the cave.

Finally, late on 6 July, rescuers set up an oxygen supply. And in the end the boys communicated with their parents the old-fashioned way - by writing letters.

The letters, made public by the Thai Navy Seals, were deeply moving.

Scrawling hearts and smiley faces on note paper, the boys told their parents again and again that they loved them and not to worry.

They listed the food they wanted to eat - fried chicken and pork crackling. One even cracked a joke: "Teacher, please don't give us too much homework!"

"I'm really sorry to the parents," said Coach Ake in his letter. But instead of a tongue-lashing, he received only love.

"Coach Ake, I really thank you for taking care of all the kids, and keeping them safe," one boy's relative wrote.

D-Day

Sunday 7 July. Two weeks had passed since the boys went missing.

Out of the blue, the Thai authorities announced they were pulling out the boys - now.

"There is no other day that we are more ready than today," Narongsak Osotthanakorn, the head of the rescue operations, said.

Journalists and volunteers were asked to leave the cave rescue site - and a brisk, steely mood overtook the camp.

Why the snap decision? The rain that had pelted Mae Sai incessantly had petered out in recent days, giving rescuers a rare break.

Locals had also told the Thai Navy Seals that by around 10 July every year, the Tham Luang cave system would be completely flooded.

It was time to launch what would later be described as a "superhuman" rescue effort, one that involved nearly 100 Thai and foreign divers.

The journey was split into two sections.

The first - from the boys' rocky ledge to chamber three - was more difficult. Rescuers made their way for hours through pitch dark waters that were bone-chilling cold, feeling their way with guide ropes. At times they had to navigate sections so ridiculously narrow that they could only just about fit a body through.

Each boy was given a full-face air mask to ensure they could breathe, and clipped to a diver. Another diver accompanied them.

A cylinder was strapped to the front of each child, while a handle was attached to their backs - and they were held face down to ensure water would run away from their faces.

John, the British rescue diver, likened the equipment to "a shopping bag" that allowed them to manoeuvre the boys around obstacles.

At the narrow sections, rescuers had to unstrap their air tanks in order to squeeze through, while also pulling along their precious cargo.

It would have been terrifying for experienced divers, let alone children who couldn't swim. The Thai government says the boys and the coach were given anti-anxiety medication to relax - but several sources have told the BBC that they were in fact heavily sedated, and only semi-conscious during the journey - to ensure they would not panic.

Once they reached chamber three, it was time for the second phase. This took another few hours.

Each boy was secured in a stretcher, and carried by a team of at least five men. At one point they had to place the stretcher on a raft and pull it across a chin-high pool of water.

Rescuers had to winch the boys up a steep slope using a pulley system. In some rocky areas they formed a human chain, passing the boys hand to hand, while at others they slid them on top of pipes pumping out water.

For diver Ivan Karadzic, the experience was extremely stressful. Stationed at a halfway point in the cave, he was responsible for replacing air tanks and guiding rescue divers through.

He clearly remembers the nerves he felt when the first boy emerged from the darkness and was brought towards him. "I didn't know if it was a casualty or a kid," he told the BBC.

"But when I saw that he was alive and breathing - it felt very good."

One by one, the Wild Boars were brought out of the darkness of Tham Luang. They were given oxygen before they were swiftly spirited away in ambulances to a hospital in Chiang Rai city.

Rescuers took them out in three batches over as many days, as they needed time in between to replenish air tanks.

But they were cutting it close. By the time the last batch of boys and the coach were out, water levels were starting to rise again, as rapidly as 30cm in one hour, according to senior Navy SEAL Supachai Tanasansakorn.

It was Tuesday 10 July - the day that locals said the cave would become completely flooded.

But while the boys were out, there were still people left on the rocky ledge deep inside Tham Luang - the Navy SEAL divers and medic who had looked after the Wild Boars, as well as Richard Harris, a famed Australian cave diving expert and doctor.

They emerged shortly after the last boy was taken out. It was not a moment too soon, as a pump suddenly stopped working - some said it failed while others said it was switched off.

Floodwaters rushed in, sending workers clearing up the site fleeing.

Hooyah!

It was an astonishing feat - after two agonising weeks the Thai cave boys and their coach were finally out at last, safe and sound.

On Facebook, the Thai Navy Seals posted: "We are not sure if this is a miracle, science, or what."

In Chiang Rai, jubilant crowds lined the streets leading to the hospital, cheering on the ambulances. Car horns blared incessantly in celebration.

Thai social media was inundated with posts hashtagged #ThankYou, #Heroes and #Hooyah, the signature Thai Navy Seal chant.

All around the world, millions of people who had anxiously followed the story celebrated the return of the Wild Boars.

But it was a bittersweet night for one person - Richard Harris. The selfless doctor who cut short his holiday in Thailand to save the boys' lives received the terrible news that his father had just died.

Reunited again

Dressed in gowns and wearing face masks, the Thai cave boys sat up in their hospital beds and waved to the world.

On Wednesday 11 July, the media got its first post-rescue glimpse of the Wild Boars in a Thai Navy Seals video. Some made victory signs at the camera.

Their parents, who had waited so very long to hold their sons again, were not by their side. They were behind a viewing window, some sobbing with joy at the sight of their boys.

The government said it was necessary to quarantine the boys to protect them and others from infection - though this did not stop Prime Minister Prayuth Chan-ocha from visiting in person. The parents did not publically object to the strict rules.

At the hospital, the boys and coach were put through a series of health checks. Eye shades were a must at first - their eyes, accustomed to two weeks of darkness, could not bear the light.

Hospital authorities said that some had minor lung and eye infections and needed antibiotics. Apart from that, they appeared to be on the mend.

Eventually, parents were finally allowed to briefly see the boys, although they had to maintain a 2m distance, and don hospital gowns and masks.

Some of the boys have even been able to start eating normal food again, after days of craving chocolate and their favourite snacks.

As for the rescuers, they are still digesting the unprecedented feat they pulled off.

"We didn't think the mission would be this successful," said Thai Navy Seals leader Rear Adm Arpakorn Yuukongkaew.

When rescue operations began, his team only had "a little bit of hope that they might still be alive".

"In the end that tiny bit of hope became reality."

Many had expected the story of the lost Wild Boars to end in tragedy. Instead, it became a story of hope and survival, and of parents and sons reunited.

It's a story of ordinary people from all over the world coming together in a remote town in northern Thailand with one mission: to save 12 young boys and their coach.

"If you could do the same for someone else's child, you would," John told reporters upon his return to the UK.

What's next for Mae Sai? The district, and Tham Luang cave, have been put on the global map, probably permanently.

Already, local officials are planning to convert the cave complex into a museum and tourist attraction - and, inevitably, at least two production companies are eyeing the Hollywood potential of the story and angling to turn the rescue mission into a film.

As for the Wild Boars and Coach Ake, plans are afoot for them to shave their heads and spend a few days in a monastery. Their families believe this Thai Buddhist tradition will bless their lives, and cleanse them of an unfortunate experience.

"It's for their protection," said Night's grandfather, Seewad Sompiangjai. "It's like they have died [after going into the cave] - and now have been reborn."

For the boys, and Coach Ake - their first priority once they leave hospital must surely be to spend time with their families again.

After all, Night still has to celebrate that 17th birthday - and his parents have promised him a party.