So let's just say I'm a medieval architect, and I'm comissioned to build a huge fortress (or renovate an old one). What were the main buildings of a fortress (not just military, but every aspect). Which were the main things a medieval architect would focus on?
Your first priority would be location.
A fortress should ideally be in a strong defensive location. This might clash with other considerations. The fortress might need to be in a town (if its purpose was to protect or dominate the town) for example. It might need to be somewhere central to the lands of the fortress owner. Nevertheless, working within such constraints, picking a location would be the prime consideration.
The location should ideally have these features:
A source of water. A castle without water is a castle that is not defensible. The castle should have a well (the best source of water, hard to divert or dam) or be on a river, or (worst of all) have extensive cisterns and systems for collecting rain water.
Storage for provisions. A castle must be able to store the provisions necessary to sit a lengthy siege. Extensive cellars or vaulted ground floors will need to be included to store supplies which are protected from damp and rodents.
Strong foundations to protect against mining operations. The best protection against undermining the walls of a castle was to build on bedrock. This was usually found high up, on hills or slopes, and not in valleys. Finding a location where you could build on bedrock and still dig a well that would reach plentiful water was difficult. If you could not find a good location with solid bedrock foundations, your next best bet would be to plan to build surrounded by water. Building on an island in the sea or lake or river was a good defense against attack, and also, resupplying a castle from the water during a siege was often more possible than by land. Defenders boats or ships might be disrupted by storms or weather and friendly re-supply vessels able to sneak in. Wet moats were the third best defense against mining operations.
Height. Building your castle on a hill or ridge, especially one not overlooked by other higher places nearby, gave considerable advantages. Your arrows and siege artillery, aided by gravity, could out-range those of attackers. Bringing troops and artillery within range of your walls might even be difficult. The height of your location acted to make the effective height of your walls greater, and reduce the chances of scaling ladders or assault towers being used against you. Of course, elevated locations had a problem, and that was that it would be more difficult to quarry a well down to the water table from a high location. Sometimes castles built on hill tops had to rely on cisterns and rain collection for a water supply, which was not as reliable or secure as a good well.
Clear field of fire. You didn't want buildings or forests crowding up close to your castle. You wanted to be able to see any assault coming and for the area outside the walls to provide no cover for attackers. This could be especially difficult in towns or cities, as the best locations might require extensive clearing of existing buildings, sure to be exceedingly unpopular with the owners, or very expensive.
A defensible approach. The main gate was almost always the most vulnerable point in a castle. Ideally this should be via a difficult approach, exposed to defensive fire all the way, and with several sharp bends or narrow places which would make getting a clear run at the gate with a battering ram difficult. Where to put the main gate, and how to defend it with drawbridges, barbicans, etc. would be a key consideration. The same would apply to the postern gate.
Signaling capability. Ideally your castle would have a line of sight to several other fortresses controlled by your client or by others likely to be his allies. The best way to make sure that sieges or assaults on castles failed was the concerted efforts of allies to make the life of the invader difficult by raids on his supplies or counter attacks so that he had to lift the siege. This co-ordination would be facilitated if other castles were visible from a high point in your castle, so signals could be exchanged by flags.
The curtain wall. Solid foundations. Thick and high walls. A broad walkway behind solid and crenelated ramparts, towers at regular intervals, allowing archers a field of fire along the base of the walls, with the only stairs to the walkway inside the towers, with stout doors breaking the walkway so that attackers could only seize a small portion of the wall if they succeeded in scaling it, combined with a second wall or keep within range of the curtain wall, and a high drop down the inner face, so successful attackers would be trapped on top of the wall and swept by arrows with no easy way down, would be the ideal to make a strong and highly defensible curtain wall.
A strong keep. Even if the curtain wall fell to an attacker, a stout and high keep with corner towers, crenelated roof defenses, strong (and usually elevated) door, well, provisions, and narrow spiral stairs (twisting clockwise going up if the client and his family are mostly right handed, or the other way if (like the Kerr family in Scotland - whose castles reflect this) they were mostly left handed - As this twist would give the advantage to defenders retreating up the stairs if the enemy broke through the door to the keep) would likely be able to keep holding out for several weeks or months even after the curtain wall was taken.
Near to good stone and available labor. Building a castle is going to be expensive, but most of the expense will be the labor of the local villagers. If your fortress is in debatable or hostile territory, and you need to import a workforce, rather than use the labor of your client's villagers, the expense will go up rapidly. Hopefully the materials, (stone, wood, tile) will be near at hand on your client's land. If there is no nearby stone, your castle will get expensive. Transporting the stone any distance is likely to cost more than quarrying it, especially if there is not a convenient waterway which would allow you to transport it by barge.
Don't forget to include a large pigeon coop in your design. This will be useful for signalling if you have exchanged some homing pigeons with your neighbors. Pigeons will also provide one of the only ways of gaining provisions during a siege. They can fly out over the siege lines to feed during the day, and they will return at night, at which point some of them can be harvested and eaten.
The final consideration for the architect would be the internal amenities, living quarters, stabling arrangements, chapel, etc. of the castle, and how to incorporate them without degrading the defensive requirements.
How close you could get to defensive perfection would be determined by how good a location you could find within the constraints of your client, and by how extensive your budget was.