A surprise court ruling that the move lacked a legal basis could lead to years of appeals and proceedings