Mail.app rules in a single script?

I have a very basic understanding of AppleScript (meaning, VERY basic).
Last night I had to sign out on my Mac and when I signed back in, all the rules on Mail.app were a mess.
Old rules I deleted a long time ago were back, the order of all rules was changed, some rules that were supposed to send emails to a specific folder were now set to send them to the Trash, some recent rules were gone. A complete mess (all because of how iCloud is such a mess itself).

So I would like to create a single rule in Mail that triggers an AppleScript that then contains all my rules. That way I can always have a backup file on my computer and I don’t have to rely on Mail’s rules (and iCloud messing with them again).

Is this possible?
My rules are all pretty simple such as:
If the email is not in the To: filed, send to XYZ folder.
Sometimes I have the “ALL” option, sometimes “ANY”.
Besides these, I use simple conditions such as “contains”, “doesn’t contain”, etc.
Nothing too fancy.

I really just want to automatically send emails to specific folders based on some conditions, send them to the Trash folder, or simply Delete them right away (skipping the Trash).