It’s idiotic to think that the US was ‘surprised’ when the jap code was already broken in 1940, earlier than the Enigma machine.
There were two codes, the Naval code and the Diplomatic code, AKA the Purple Code. The purple code was brown to a degree, the naval code not.
I have read a a few books on this and while there was a heightened level, and there were some terrible mistakes, one being Admiral Richmond Turner and his responsibility in this fiasco.
https://en.wikipedia.org/wiki/Richmond_K._Turner
The whereabouts of the Japanese Fleet was unknown and at one point the thinking was they would attack the Aleutians, not Pearl Harbor.
All in all a very confusing period.