SAN DIEGO — Lucas Duda, the slugging first baseman who improbably became one of the Mets’ longest-tenured players, was traded to the Rays on Thursday. The deal signaled the start of a retooling triggered by what has been a season of unmet expectations.

The Rays sent the Mets minor-league righthander Drew Smith, who owns a 97-mph fastball and a power curveball that someday could make him a key piece in the bullpen.

The trade went down before the Mets’ 7-5 loss to the Padres, who knocked around Chris Flexen for four runs (three earned) and five hits in a major-league debut that lasted only three innings. Flexen, 23, walked four as he became the first Mets starter since Mike Pelfrey in 2007 to jump from Double-A to the big leagues.

“Obviously, it didn’t go how I wanted it to tonight,” said Flexen, who allowed a leadoff homer by Manuel Margot and left the game with the Mets down 4-1. “But it was still a good learning experience.”

Trading Duda established that on-field results will be mostly secondary. With the non-waiver trade deadline coming Monday, the Mets clearly have shifted their focus from the present to the future.

Smith, 23, has a 1.60 ERA in 31 games this season between four different levels in the Rays’ organization. He was the system’s 30th-ranked prospect, according to mlb.com.

advertisement | advertise on newsday

According to a source, the Yankees made a push for Duda and remained in talks until the end. But the Rays pulled off the deal and will have Duda as they face the Yankees this weekend.

“I was very proud to be a New York Met,” said Duda, who had been the team’s longest-tenured player aside from David Wright. “And I’m going to be very proud to be a Tampa Bay Ray.”

Duda, 32, is hitting .236 with a .357 on-base percentage and 17 home runs in 85 games this season and will reach free agency at year’s end. It’s what made him a trade asset for the Mets, who hope to recoup some value from their slew of players on expiring contracts.

The Mets chose Duda in the seventh round of the 2007 draft, the start of a career marked by ups and downs. He was miscast as an outfielder before finding a home at first base, though he nearly was traded because of a logjam at the position. Instead, the Mets dealt Ike Davis in 2014. The decision was made partly because of Duda’s elite exit velocity numbers, though it came at a time when such data had yet to enter the mainstream.

Duda hit 30 homers in 2014 and had 27 in 2015, when he was an integral part of a team that won the National League pennant. During the World Series, he carved an infamous place in franchise history when his throwing error helped the Royals win the clincher in Game 5.

Mets videos

Still, his 125 homers rank seventh in franchise history.

“Certainly, he was a tremendous part of this team for a long time,” manager Terry Collins said. “We wish him all the best and thank him for everything he’s done here in New York.”

The Mets now have a clear path to promote one of their top prospects, first baseman Dominic Smith, a former first-round pick. “He’s going to be good, really good,” Duda said. “And I hope he turns out to be a 10-year All-Star, so best of luck to him.”

A source said Smith’s promotion isn’t imminent. Top prospect Amed Rosario is slated to arrive first, shortly after the trade deadline.

Less than an hour after his cellphone rang and he heard general manager Sandy Alderson’s voice, Duda seemed to be coming to grips with the move.

advertisement | advertise on newsday

“Are you guys taking batting practice?” he said as he passed his former teammates in the hall. One of them was Jay Bruce, who played first base Thursday night and hit his 26th homer, a two-run shot. Bruce wore James Loney’s old first baseman’s mitt because his own has yet to be broken in.

Shortly after saying his goodbyes to teammates, Duda said he had “mixed emotions” about leaving the Mets, an organization he called “first class.”

Curtis Granderson, who became a close friend, posted a video of his reaction to the news. It showed Granderson on the ground refusing to let go of Duda’s leg as he walked out of the clubhouse.

Said Duda: “I’m looking forward to starting a new step.”