Well a bad battery connection probably wouldn't kill a running engine.
While you should never do this on newer electronic systems, you can disconnect the battery while engine is running and it will stay running, that used to be an "old school way" to test if alternator is working, lol, but bad idea to do that now as there can be a voltage spike when reconnecting as alternator would be at full output.
I would look at the ignition switch first, under the steering column.
It gets power via a fuse from the battery(or alternator when running).
That power is sent(when key is on) to all the systems and EEC relay.
The EEC relay is what powers all the engine systems, coil, injectors, fuel pump relay and computer, so EEC relay losing power would kill the engine mid-stroke
, same as turning off the key.
The ignition switch is a "slider" type switch, rotating the key slides the contacts into making connections, dirty or worn contacts could cause intermittent issues and loss of power.
Also check the power wire going to the ignition switch.
The EEC relay is either on the passenger side wheel well or under the power distribution box, there are usually 3 relays in a row, the brown one is the EEC relay, green is fuel pump relay.
If that relay or its wiring is going bad it would cause similar issue.
Brake lights and head lights are separate from ignition switch, so if those still work after shutdown then it wouldn't be battery issue.