Mail app rules

I had a good list of Mail rules (around 10 or so) that were ordered a certain way to successfully organize my incoming emails.
Last night I had to sign out on my Mac (Apple ID sign out) and when I signed in again and went to Mail, some of the rules were simply gone, old rules that I deleted a long time ago came back, and the order is all messed up.
Even some rules that were initially set to send to a specific folder, are now set to send the emails to the Trash. What a mess! iCloud seems to be one of the worst Apple product...

Question: can I have KM apply certain rules for incoming emails instead of relying on Mail's rules?
I saw some posts mentioning AppleScript, but I'm not that advanced to create those or to even know if it's possible.

Even if the solution is to run a single script that then reads all the rules inside that script, at least I know I have access to all the rules in a single script I can save on my computer as a backup, instead of relying on iCloud managing this for me.