It's unfortunate. It's been suggested that Erdogan is trying to get political votes from his conservative and nationalist supporters while Turkey is going through inflation and economic uncertainty caused by coronavirus. Typical political stunts pulled by governments - this decision has increased tensions with Greece.
The Hagia Sophia case was that Ataturk's cabinet had acted illegally in 1934 by converting it from a mosque to a museum as it was the property of Sultan Mehmed II. Hagia Sophia was turned into a mosque after the 1453 conquest of Constantinople by Sultan Mehmed II after 900-years of it being a Byzantine church.
“It was concluded that the settlement deed allocated it as a mosque and its use outside this character is not possible legally,” the Council of State, Turkey's top administrative court in Ankara, said in its ruling.
There's just been another case, with some similarities to the Hagia Sophia decision, heard in the Turkish courts to cancel the museum status of Istanbul's Kariye Museum, another Orthodox church, that became a mosque half a century after the 1453 conquest of Constantinople and then a museum after WW2. This courts have allowed the museum to be turned back into a mosque.
Apparently it is expected that the ancient mosaics depicting Christian figures in the site will be covered with curtains during Muslim prayers, just like how it’s now being done at the Hagia Sophia. Let's hope that tourists will still get to see the mosaics and walk around when prayers aren't being conducted.