Say I have an AC circuit with rms voltage $\mathcal{E}_{rms}$, reactance $X$ and resistance $R$. We may say that the average power dissipated in the circuit is as follows:
$$P_{avg} = \boxed{\mathcal{E}_{rms}i_{rms}} = \mathcal{E}_{rms}\left(\frac{\mathcal{E}_{rms}}{Z}\right) = \frac{\mathcal{E}_{rms}^2}{\sqrt{R^2 + X^2}}$$
where $Z$ is the impedance of the circuit. Now, the formula in my textbook hinges on the fact that any power dissipated will be through the joule heating only. Therefore, the average power may be written:
$$P_{avg} = i_{rms}^2R = \left(\frac{\mathcal{E}_{rms}}{Z}\right)^2R = \mathcal{E}_{rms}i_{rms}\frac{R}{Z}$$
This is clearly different from the boxed formula. Why is this? Is $P=VI$ not valid for ac sources? (I don't see any reason why that should be). Does the second formula disregard any energy losses? (through EM radiations, maybe). This is supported by the fact that the second formula reduces to the first when $R=Z$.
Also, this is a bit unrelated but what causes power to be dissipated in the primary coil of a transformer?
In a transformer, the primary coil is essentially a purely inductive ac circuit. How does the energy leave the circuit?