Unified first-principles theory of thermal properties of insulators