I don’t think Mattis or much of that first cabinet realized how obstinate and uninformed the POTUS is in foreign affairs.
I will offer to you an alternate thesis: it didn't matter.
I can't find the graphic because of google indexing, but Presidential administrations tend to pass around high-level cabinet members and SMEs like the village bicycle. The typical example would be Donal Rumsfeld. But there are many, many more.
Trump, even in his first term, appointed people who had
no prior association with another administration. There might be 1 or 2 who did. He wanted 'new blood,' but being an inexperienced President, generally chose people who were esteemed.
So here's the rub: Mattis is a stereotypical neocon, and 'came of age' in the Bush administration. Trump is...not that. And I postulate that Mattis knew Trump's foreign policy views weren't completely in alignment with his, but he had an opportunity to 'jump the line' of career administration officials to propel his career rapidly. And he had an idealistic view that maybe he could reason with the man by leveraging his in-depth experience and joint education.
Now, Mattis eventually found his moral principals were more important than his job responsibilities and political career, so he resigned. But that doesn't change the fact that Mattis initially accepted the position out of idealistic career opportunism.
Mattis would have done well if he were the SECDEF for Reagan, Bush Sr, or Bush Jr. But he was not working for those men. It isn't Trump's fault that Mattis accepted an advisory role to someone who fundamentally disagreed with his foreign policy views and couldn't get onboard with 'getting to yes.' It is Trump's fault for picking Mattis and expecting more obedience than he got... a theme that resonates with his first administration in a general sense.
As the expression goes... when there's a 'personality conflict,' the boss has the personality and the subordinate has the conflict.
Fast forward to Trump 2 and he has a mentality of "I got this," so his administration largely consists of a bunch of clueless millenials trying to make a splash. Because without Trump, they wouldn't get this opportunity for another 15-25 years, if they ever get it at all (Hegseth certainly wouldn't).