Any state in Hilbert space $|\phi\rangle$ can be expressed in terms of a complete basis $\{| v_i\rangle, i=1,2,...\}$ as $$|\phi\rangle=\sum|v_i\rangle \langle v_i |\phi\rangle . $$ Now, if I understand this correctly, Hilbert space has the dimensionality of the complete basis. In other words, if any state can be expressed as a linear combination of, for example, three orthonormal vectors, then Hilbert space has dimension $d=3$.

Now, in many examples of QM the wavefunction can be expresed in terms of a discrete basis (like the energy eigenstates): $$|\phi\rangle=\sum_i|E_i\rangle \langle E_i |\phi\rangle , $$ or in terms of a continuous basis (like the momentum eigenstates): $$|\phi\rangle=\int|p\rangle \langle p |\phi\rangle dp . $$ Is this correct? How can any state in Hilbert space be expressed in terms of a discrete sum and an integral? What is the dimension of Hilbert space then?

Also, the $\{|p\rangle\}$ doesn't behave like the discrete basis because the state $|\phi\rangle$ can't just be any linear combination of this basis.

There are some posts here that are related to this question but none of them point specifically to this issue. For instance here a similar question was addressed. However, the context of that question implied that the basis functions must be square integrable, which then resolved the issue. However, here the question is placed in a broader context that also allows basis functions that are not square integrable, such as for instance the momentum basis. So I hope it can help to enlighten the problem.