In D&D rules, you cannot use bonus action for offhand attack, unless you already made melee attack with main hand this turn (either with action, or haste's extra action).
In short you can't cast spell and do offhand attack (with exception on when spell actually perform melee attack as part of spell).