Three main approaches to such a thing.
1) Traditional control editing. This has two, maybe three approaches as well.
2) OS level editing.
3) Controller level tweaking.
In all cases mapping stick/analogue movement to button presses and vice versa can be tricky, though not necessarily impossible.
Going in reverse order.
Controller level stuff is much as it sounds. Here you open up a controller, take whatever button to whatever other button by cutting traces and throwing wires around. Those doing controllers for disabled people, rapid fire mods (see test points) and the like being what you probably want to look at for a start. To do more complicated actions you then get to investigate programmable micro controllers and such like (seen arduino, teensy family and other such things -- the blinking light patten that such things using for their basic intro tutorial being about as radical as most coding needs here).
Costs a controller per player and some time but works on any game, any console, no hacks needed, and depending upon how good you are can be reprogrammed to do other things.
2) OS level stuff is gained by more recent devices. Anything without an OS running in the background can't do this. Never the less many hacked firmwares/operating systems for things are this. I don't know enough about PS4 hacks to know what goes here but it is fairly common for most things that gain hacked firmware/software level hacks.
PC can do this sort of thing trivially, amusing video covering the general premise and a not unreasonable method.https://www.youtube.com/watch?v=lIFE7h3m40U
I don't know what the kids are using instead of joy2key this week. The software layers in the video above also being pretty good for macros. You can also drag back your teensy family device in this if you had one from 1) as they can function as a keyboard and mouse.
3) Game editing, naturally needs the means to run a custom game.
As mentioned three main avenues of attack
i) Debounce area editing.
Consoles have the data for the controller appear in memory somehow (serial, packet, parallel... you can consider those if you want but ultimately it ends up in memory). However most won't use it directly from memory -- possibly too slow, and bounce issues. Bounce is a failure mode in switches but also a general concept; ever had a mouse double click on you when you had only clicked once, this is switch bounce, even healthy switches however do it on millisecond levels where it might flick a few times between pressed and not before settling. Very annoying for users where things are measured in milliseconds for various things or might have two things depend upon a button press that might be pressed for the first aspect but not when it goes to check again.
This debounce area is generally normal memory and thus you can go in and change bit 1 (call it X in this hypothetical) to be high and instead have bit 3 (O in this example) be low when bit 3 was originally high and vice versa (this is your X-O swap). Probably going to be game wide rather than specific things, and won't have corresponding on screen tweaks unless you do those separately.
ii) Traditional editing.
In the game will be a function that checks a button press (or more likely is an interrupt as checking all the time is dubious use of computing resources) and correspondingly does an action. You tell it to respond to another button aspect and bam.
Needs every action you want changing to have this done to it.
This would also be how most add serious extra functions/features to games (diagonal movement in games without it being the classic example, however your one for potions if it is not something a simple macro can do might be one).
Depending upon what you are doing this is a reasonable assembly level hack-- something with a nice definitive action (jumping, consuming something -- the sort of thing that has a definite effect on some area of memory you can find with a cheat or basic analysis) that you can latch onto and work backwards from that point (use a potion and somewhere in the preceding instructions before the potion counter is reduced by 1 dealing with the sound, animation and whatnot will be the button press it looked for in the first place). If there is not already an invincibility move then you get to make one, and in that case we get to talk about graphics editing and hitboxes (bit basic but a nice intro https://www.pcgamer.com/uk/how-hitboxes-work/
) which is a far bigger ask -- while limits for ROM hacks are "whatever you can make the device in question do" then more practically "something the game already does" tends to be the limit before things get hard.
iii) Caveat for in game custom controller setups, even basic ones where you can only swap between premade schemes. These are something of a combination between i) and ii) and the game will have its own scheme to go between bounce and actions happening. Find this scheme and you can edit things, for more customisable stuff than premade then a lot of the time restrictions are just things the dev put in there because they thought nobody would want that and as such you might be able to poke it with a cheat to have it be what you think is necessary.