  We study the cosmological evolution of a complex scalar field with a
self-interaction potential $V(|\varphi|^2)$, possibly describing
self-gravitating Bose-Einstein condensates, using a fully general relativistic
treatment. We generalize the hydrodynamic representation of the
Klein-Gordon-Einstein equations in the weak field approximation developed in
our previous paper. We establish the general equations governing the evolution
of a spatially homogeneous complex scalar field in an expanding background. We
show how they can be simplified in the fast oscillation regime and derive the
equation of state of the scalar field in parametric form for an arbitrary
potential. We explicitly consider the case of a quartic potential with
repulsive or attractive self-interaction and determine the phase diagram of the
scalar field. We show that the transition between the weakly self-interacting
regime and the strongly self-interacting regime depends on how the scattering
length of the bosons compares with their effective Schwarzschild radius. We
also constrain the parameters of the scalar field from astrophysical and
cosmological observations. Numerical applications are made for ultralight
bosons without self-interaction (fuzzy dark matter), for bosons with repulsive
self-interaction, and for bosons with attractive self-interaction (QCD axions
and ultralight axions).
