There's definitely other people who can answer this a lot better (I.E those who have actually looked at it or worked with it), but as far as I know, It basically assigns scores to each of the options it has available each turn (moves, items, switches) and increases/decreases each of those scores with various things like an estimated damage calculation of each move, what moves the opponent has used and if it outsped the last turn, what happened the last time that action was taken if it's been taken before, how it might affect the field, etc etc. Then it uses whatever option has the highest score after all those checks? I think many of the significant trainers also have some extra bits that change the weighting of certain options to make them use their specific strategies better, like Radomus more strongly prioritizing setting trick room when it's not up for example? iirc there's also a couple things currently that can sometimes make it easier, like it trying to save PP by using a weaker move when you're low, which could make them suffer if you heal, or adding extra weighting to a move that only has a chance to KO over one that's guaranteed to maybe create a tense moment (though my memory is hazy on this one being mentioned, so it could easily be wrong). Sometimes these can instead help the AI catch you out on a switch though too.