Is a firm profit-maximizing if it is cost-minimizing?

Hi guys, I am new here. I am confused about profit-maximizing (MR=MC) vs cost-minimizing (MC=AC). They seems the same only for perfect competition long term equilibrium when P=MR=MC=AC. Can anyone explain it? Thanks!

Michael