SAN FRANCISCO (AP)Versatile infielder Wilmer Flores and the San Francisco Giants agreed Monday to a contract that guarantees him $16.5 million over the next three seasons and could be worth up to $21.5 million.

He opted to stay in a place he’s comfortable rather than test his value on the free-agent market this offseason.

”It was a no-brainer. Since day one, I’ve been just loving the culture here and the way they treat me,” Flores said. ”I just wanted to come back here. I knew I had a chance to go to free agency but I just love everyone here and playing on this team in San Francisco makes me feel good. You can’t buy that. We wanted to get it done and we got it done.”

Flores will receive salaries of $6.5 million each in 2023 and 2024. The deal includes a $3.5 million player option for 2025 that must be exercised within three days of the end of the 2024 World Series. The Giants have a conditional $8.5 million club option that can be exercised within five days of the end of the World Series if Flores declines his option. There is no buyout.

The 31-year-old Flores played in his team-leading 133rd game Monday night against Atlanta, playing third base. He had a Giants-best 65 RBIs and his 18 home runs are second-most on the club while matching his career high set in 2017 with the Mets.

”It has been a nice fit and he expected it would continue to be a nice fit even as this team evolved going forward,” Giants president of baseball operations Farhan Zaidi said. ”Happy that he was willing to get something done and we certainly were and that’s usually when sides come together.”

Flores said talks began about a month ago. The Giants are thrilled Flores wants to stay after they fielded about a half-dozen inquiries about him at the trade deadline.

San Francisco manager Gabe Kapler praised Flores’ loyalty, work ethic and consistent example for others as a regular taking early batting practice.

”I love to see players who do things the right way over a long period of time get rewarded for not just the performance between the lines but for the preparation that leads to the performance between the lines,” Kapler said, ”and nobody prepares better than Wilmer.”

Flores is playing this season under a $3.5 million team option, completing a three-year deal worth $9.5 million. He is hitting .235 and has matched his career high of 18 homers and set a career best with 65 RBIs. He leads the Giants in RBIs, runs (66) and games (132) and is tied for the team high with 18 doubles.

Flores has a team-high 48 homers in his three years with San Francisco, playing first, second and third.

He will make a $32,500 annual donation to the Giants Community Fund in each of the next two seasons, and an additional donation in 2025 if he plays under an option year.

”You can’t buy where you feel comfortable, where you feel at home, where you already know everybody,” Flores said. ”I’ve been on three teams already. Getting to know people, it takes a lot of time and it takes some getting used to. Staying in one place, it was the No. 1 thing.”

AP Baseball Writer Ronald Blum contributed.

More AP MLB: https://apnews.com/hub/MLB and https://twitter.com/AP-Sports