Toyota Motor Corp. is in discussions with authorities in Shanghai about the possibility of constructing a wholly owned factory, according to people familiar with the matter, in what would be a risky gambit for the Japanese automaker at a time it’s struggling in China.