How to create a valid BTC Block?
Your question involves quite a bit.
Mining is done with conjunction of a wallet.
A lot of the work is handled by the wallet.
See the RPC getblocktemplate
Also run help getblocktemplate to see more information, that will point you to a few BIPs (B22, 23, 9, and 145) for further reading.   

Basically (simplified) you do a getblocktemplate and then you repeatedly hash it (while incrementing the nonce) to get a hash that meets the difficulty criteria.

I hope this is enough information to get you started