The problem was probably due to the fact that I created a new Mac User and I was trying to sync contacts taken from previous user.
I managed to fix everything in this way:
disabled iCloud for contacts, calendars and remainders both on Mac and iPhone
deleted all contacts from iCloud.com (I had to delete a few at a time to be successful)
deleted all contacts from iPhone
deleted all contacts from Mac
with iCloud turned off both on the Mac and the iPhone, I imported all the contacts from an exported .vcf file I created beforehand, into the Contacts app on the Mac
I browsed all 3,000 contacts to check duplicates and invalid names
I then synced manually the contacts on the Mac with the iPhone via usb cable and the finder window
now I could see all the names in the apps like iMessages or WhatsApp
I switched on iCloud on the Mac and copied the contacts into the iCloud group in Contacts, but this I had to do in small batches otherwise it didn't work
wHen finished, I merged the two groups
then I switched on the iCloud contacts on the iPhone and now I have all the contacts synced automatically via the cloud service