This is a potential can of worms but here it goes.
The invasion at bay of pigs WAS intented to overthrow castro, not right away, but as the start of an insurgency campaing that would eventually topple the regime. Remember, castro came to power after launching his own invasion with a mere 80 men. A few things went wrong, for one as mentioned before, the US government reneged on its promised air support.
But more importantly, castro got wind of the coming invasion and in the weeks before the invasion rounded up several tens of thousands in Cuba who might have joined the invaders and/or provided support.
This was important because while castro is generally portrayed as the leader of the Cuban revolution the truth is far, far more complicated. castro’s forces NEVER strayed beyond the Sierra Maestra mountains the extreme eastern end of the island, some 1100km from the capital. Attacks on batista’s government, and the most effective part of the revolution, were carried out by an alphabet soup of organizations, which organized general strikes, bombing campaigns, sabotage, and demonstrations.
Once batista was forced from power by the daily attacks, and the withdrawal of foreign support, castro shrewdly marched across the island and assumed the role of conquering general, mostly by force of personality, and by virtue of the arms his couple of hundred men had. By the time he arrived at havana, most of the Cuban military had been disarmed, imprisoned, or had otherwise fled.
In a few short weeks in 1959 castro managed to accuse the leaders of the many resistance groups of all sorts of crimes and imprisoned or executed most of them. As they said in france, “the revolution devours its children”. In the end he was left as 1. the only recognizable face of the revolution, who 2. controlled all the arms.
Consequently, when castro got wind of the bay of pigs invasion, his first order of business was to preemptively destroy any potential insurgency groups. This lead to the imprisonment of priests, leaders of student groups, intellectuals, and so on.
So now you have the scenario, a known attack, at a known location, met by a waiting force, and with no internal support. The result, in a 72 hour battle the invading force was defeated.
You might ask, how did castro know? either Cuban or russian spies in the US government is the most likely answer. As an illustrative example I give you Ana Montes, the US government’s foremost Cuban expert and the author of a number of Cuba policy recommendations. She was arrested shortly after September 11th, she had been spying for Cuba for a number of years (she was of puerto rican descent). Sentenced recently to a whole bunch of years in prison. Not that she had anything to do with this subject, just an example of how high a spy can get.