@rsonx file names have to be unique in a directory. The directory identifies a file using file name(the directory translates file names into directory entries). Two files can have same name only if they are in different directories or sub-directories because now they will have unique paths.
So why would we need unique paths you might ask, the answer lies in how file system uses a file. To use a file (read() or write()) we first need to open it, which is done via open() system call which requires path of the file as one of the argument.
https://man7.org/linux/man-pages/man2/open.2.html
The above question is only concerned with a single directory named foo. So all the file names in this directory has to be unique.