python: What algorithm should I use to create an automatic sports calendar function?

I have in mind to create (I would like to do it with python) an algorithm that automatically creates a palimpsest for a sports tournament.
I would like to give as inputs the maximum number of matches, the maximum number of available fields where the teams play at the same time and the number of teams. The output will be a calendar with the maximum possible number of matches.
I will use this for the children's basketball tournaments, so keep in mind that the teams that are eliminated from the tournament still play so that everyone plays the same number of games.
But I will also use it for adults, so who is out of the tournament does not play anymore.
I would also like, in case the user does not put the maximum number of matches, the output will be the ideal number of matches in both cases.
I have an idea to do it recursively, but first I go ahead. I would like to have an opinion of who has more experience than me … I would not hit my head against a brick wall. Thank you