I should have consulted a lawyer.

Literal

Lawyer [with-に] consult [do-in-advance-ておく] [should have-べきだった].

A stack of useful endings. ~ておく ('do in advance, do beforehand') marks the action as something prudently arranged ahead of time. ~べき expresses obligation/propriety ('should'); ~べきだった is the past form for regret about a missed obligation: 'should have.' The whole thing: 'should have consulted (in advance).'