I think that if the Bible is a book that God wrote, then God wrote in sin because most of it is about sin. Why did God write a book on purpose about sin?
Why didn't God write a book about righteousness?
Of course I do not believe God wrote the Bible. Human's who believed in God wrote their thoughts 'as if' God wrote them (regardless of whether they understood that as such or not). The pen was in the hands of people, not God. But why would they focus so much on sin? Multiple reasons, of course. First look at the 'law' as a tool of governing a people. Why do we have "sins" listed in our law books?
Because it's about establishing the rules of a society, and systems of law enforcement to ensure the society functions. You don't have "happy thoughts" in law books. So that's the first reason.
The next reason is that of the poets and mystics existential dilemmas. With God as the symbol of Ultimate Reality, and with them living in this world, both sensing the divine as well as their own sense of separation and isolation from this Unity with God, they symbolically expressed this in a cause and effect relationship. From this comes the myths of the Fall of man, once having had Unity in an imagined past, with some event that caused that change in state to the current one they now experience, longing for Unity, yet experiencing separation. They are stories to express this condition, this realization of isolation in themselves. So symbolically, they seek ways to reunite with God, to appease him, to attempt to overcome themselves and the causes of this separation. So that second reason is to essentially give themselves a set of tools, attitudes, actions, etc that help them move to realize God in their lives instead of 'hell' or separation.
The other reason would be control. To take the "rules" of a society and use the authority of the divine Absolute to "hate" sin, makes one afraid to step outside the boundaries of the society and its laws. It's not just the fear of law-enforcement, but the "divine eye" who sees all and knows all who will send locusts and boils to punish you, or burn you eternally in hell if you think you're getting away with something society doesn't like. God is the invisible "Big Brother" who monitors your very thoughts and will put you in jail for questioning authority.
There are many diverse layers of all that goes into and is used in such a book. The error is when people think it's all one thing, for one single reason. That's a mythology to make it a tool of control, rather than something which can be used symbolically to inspire positive change. In religion the talk of "sin" is much more about control, than any true existential yearning. When you hear the positive things, that is coming from those who are functioning at the internal, existential level, rather than approaching life from the external rule/role level. When you hear, "God will reward you for being good", that's generally the external Law-Giver/Punisher God, and the motivations are not internal and existential, but external social and cultural.