Optimized random-phase approximations for arbitrary reference systems: Extremum conditions and thermodynamic consistence