The Yankees have traded starting pitcher Sonny Gray to the Cincinnati Reds, the teams announced Monday, finalizing a days-in-the-making deal that ends Gray's topsy-turvy stint with New York.

Both sides completed a deal that was contingent upon the Reds reaching a contract extension with Gray, who agreed to a three-year, $30.5 million extension that includes a $12 million club option for 2023, a source familiar with the deal told ESPN. Combined with his $7.5 million salary this season, Gray's deal can max out at five years and $50 million.

New York dealt Gray and minor league left-hander Reiver Sanmartin to Cincinnati for second-base prospect Shed Long and a high draft pick, then flipped Long to the Seattle Mariners for center-field prospect Josh Stowers.

Gray, 29, will reconnect with his college pitching coach, Derek Johnson; move closer to his Nashville, Tennessee, home; and bring a career 53 percent ground ball rate to the bandbox of Great American Ball Park.

The deal is a decided risk for Cincinnati, as it tries to transition from rebuilding to contending and banks on Gray after a difficult year-and-a-half stretch in the Bronx. Yankees general manager Brian Cashman publicly stated his intention to deal the former All-Star after the team's season ended with an American League Division Series loss, and though it took more than three months, he found a willing partner in the Reds, who have remade their roster via trade this winter.

This is their third significant deal, after acquiring starter Tanner Roark from Washington, then sending Homer Bailey's dead-money contract and a pair of prospects to the Los Angeles Dodgers for starter Alex Wood, outfielders Yasiel Puig and Matt Kemp and catcher Kyle Farmer.

Gray was a prized acquisition when the Yankees dealt prospects James Kaprielian, Dustin Fowler and Jorge Mateo to the Oakland A's for him at the 2017 trade deadline. After pitching reasonably well down the stretch despite a 4-7 record, Gray returned in 2018 with an egregious home-road split that grew severe enough he eventually lost his rotation spot and didn't make the team's postseason roster. In 15 games away from Yankee Stadium, Gray struck out 78 and walked 22 in 71 innings, with hitters slashing .226/.295/.320. Their numbers against him in the Bronx were staggering: .318/.406/.527, leading to a 6.98 ERA in 15 games.

Cincinnati believes enough in Gray to guarantee him a deal into his 30s -- and to give up a well-liked prospect in Long, a 23-year-old second baseman, and a draft pick that will wind up in the late 30s and is seen in the industry as being worth about $10 million. Stowers, 21, was a second-round pick of the Mariners out of Louisville in 2018 and showed an impressive glove and plate discipline in the short-season Northwest League.

Gray could headline a rotation in far better shape than the one that finished 2018 with a 5.02 ERA: Gray, Roark, Wood, Luis Castillo and Anthony DeSclafani, with Tyler Mahle, Sal Romano and Cody Reed also capable of starting. While the Reds have yet to sign a major league free agent this winter, the additions through trade have pushed their payroll above $120 million, which would be a franchise high. Following a 67-95 season, the Reds felt close enough to start moving toward contention, even with a loaded National League Central that includes three potential playoff teams in Chicago, Milwaukee and St. Louis, as well as a Pittsburgh team that finished above .500 last season.

Gray arrived with Oakland in 2013, a little more than two years after the A's chose him 18th overall in the draft out of Vanderbilt. He slipped to the middle of the first round because teams feared his 5-foot-10 height would limit his effectiveness. His 93 mph sinker and hammer curve ensured that would not be the case, and in 2015, his second full season, Gray finished third in Cy Young voting and looked primed to become one of the best pitchers in the AL. Then came a disaster of a 2016 season, which preceded the trade to the Yankees. They believed he would be more 2015 than 2016, and the Reds are hoping the same after outflanking the San Francisco Giants and others interested in acquiring Gray.