So the question is: why does the Deuteronomistic historian who’s credited with shaping the Book of Samuel give us a complicated David, whereas the Book of Chronicles, which is later, cleans it up and gives us a different version?
I think what’s clear in Samuel is that there is ambivalence about kings and about persons in power, and the Bible is very careful about not glorifying heroes. There’s an anti-heroic thrust to it that we see again and again so that the David figure is not allowed to be this perfect, successful person.
But we are invited to see the problems with kingship, the problems with persons who have too much power and don’t know when to stop. And indeed, the whole Bathsheba affair is a case of not knowing where the boundary is and putting yourself as king above the law. And that is what the Bible is really worried about. And in fact, in Chapter 8 of I Samuel, we have this long discourse against kingship. What will a king do—and the king will subjugate you and your families and amass wealth to himself. And indeed, this is a cautionary tale.
So the Book of Samuel uses David, who is the most famous charismatic figure, to also raise the problem about this whole enterprise.