> What is the mathematical statement for the first law of
> thermodynamics, accounting for kinetic energy, potential energy,
> internal energy, work, heat and most importantly taking into
> consideration the work-energy theorem?

The general form of the first law for an closed system (a system where no mass crosses the system boundary) is

$$\Delta E_{tot}=\Delta U+\Delta KE +\Delta PE= Q-W$$

Where the terms are defined in the figure below that illustrates a general closed system. 

The work $W$ term includes the work that crosses the system boundary (that associated with the change in internal energy) as well as the external work on the system as a whole. It is the net external work done on the system as whole where the work energy theorem applies, i.e.,


$$W_{net-external}=\Delta KE$$

> Also, is $∆U=∆Q-∆W$ only valid for systems whose center of mass is at
> rest in an inertial frame, or is it also valid for other systems?

It is valid whether or not the center of mass is at rest or moving in an inertial frame, because $\Delta U$ only applies to the change in internal kinetic and potential energy of the system at the atomic/molecular level. 

For example, the temperature of your cup of coffee, which is a measure of its internal kinetic energy, doesn't change if you drink it while standing on the road, or in your car traveling at a constant velocity with respect to the road. But the kinetic energy of the cup of coffee as a whole is zero with respect to the road when you are standing on the road, and 1/2$mv^2$ with respect to the road while driving the car.

> but the work energy theorum states that work done by all the forces
> whether internal or external equals change in kinetic energy, so why
> are we considering only external work? Is there a difference between
> "work" in thermodynamics and in mechanics?

There is a difference between the thermodynamic boundary work done by or on the closed system (the work done in expanding or compressing the boundary of the system) denoted as $W_{sys}$ in the diagram and the external work done on the system as a whole, denoted as $W_{ext}$ in the diagram. 

The boundary work $W_{sys}$ can increase or decrease the internal kinetic and/or potential energy at the atomic/molecular level per the version of the first law that doesn't include changes in kinetic or potential energy of the system as a whole. An example is a gas in a cylinder fitted with a piston undergoing expansion or compression but with the cylinder being at rest with respect to the external frame of reference depicted in the diagram.

The external work $W_{ext}$ would be due to an external force that results in a change in velocity and/or elevation of cylinder of gas with respect to the external frame of reference in the diagram.

The work energy theorem is applied to the latter, but generally not to the former, though I suppose it could be argued that it applies in the sense that the expansion or compression of the gas can result in a change in the average kinetic energy of the gas molecules, as reflected by an decrease or increase in the temperature of the gas. 

Hope this helps.


[![enter image description here][1]][1]


  [1]: https://i.sstatic.net/8ule6.jpg