Fix Rotation of Origin
It's 'easier' than it used to be, here's one way, using the 'View' Transform Orientation.


Find a reference rectangular face, preferably facing down your intended Z, select it in Edit Mode, and set the Transform Orientation to 'View'
Orthographic: ShiftNumpad 7 align your view to the face.
ShiftS > Cursor to Selected .. now the cursor should be centred on the face.
In Object Mode, with the Header > Options set to 'Origins', move the origin:  ShiftS > Selected to Cursor
Header > Object  > Transform menu.. Align to Transform Orientation. Now the Origin shoould be aligned with the view, and the face.


There may be some 90 degree swivels to do while in the view, to get your X,Y and Z on the right axes, and don't forget to switch the 'Origins' option off.