First-principles calculation of the structure and magnetic phases of hematite