SNP ministers face potential legal action after blocking one of the most unpopular planning applications in Scottish history.