The Padres spent most of this offseason aggressively retooling the worst offense in the majors.

But in the week before camps opened, the Padres could not ignore a pitching gift that essentially showed up at their doorstep. James Shields could not lasso the nine-figure deal he was pursuing, so the next best thing was to take a good deal close to home — and home for Shields is San Diego.

Shields and the Padres reached agreement on a four-year contract, with an option, worth $75 million.

In the end, it was the Padres and teams with key executives who had history with Shields that appeared late in the bidding for him.

Last week, the Cubs made a three-year offer with an option in the $60 million range. The Marlins — ranking perhaps only behind the Padres for aggressiveness this offseason — continued to try to add Shields. Because of Shields’ desire to be on the West Cost, his camp courted the Dodgers, who monitored the situation just in case a sweetheart deal was possible.

The Cubs are managed by Joe Maddon, who managed Shields with the Rays. The Marlins general manager is Dan Jennings, who was the Rays scouting director when Shields was drafted in 2002. The Dodgers’ baseball operations are run by Andrew Friedman, the Rays general manager who drafted and developed Shields.

The Blue Jays were rumored to be players in the Shields derby, but had not spoken to his camp in the past two weeks.

Shields was the final major free agent remaining this offseason. Teams involved early in the process say his representative was trying to deliver a five-year contract worth as much as $125 million. But the industry was more conservative, worried about Shields’ age (33) and about what has been his greatest asset — his ability to throw significant innings. Shields has made at least 31 starts in each of the past eight seasons, raising concerns about how much is left in his arm.

The Padres are wagering there is plenty remaining. They risk spending big on an aging pitcher when it can be argued their huge park will always give them a pitching edge and their major outlays should be spent on hitters. After this season (when the Dodgers are paying $18 million of Matt Kemp’s contract), San Diego will be spend roughly $36 million annually on the injury-wrecked Kemp and the aging Shields.

Nevertheless, the decision — under new general manager A.J. Preller — to aggressively accumulate talent this offseason with Kemp, Shields, Justin Upton, Wil Myers and others has turned the Padres from one of the majors’ least relevant teams to the talk of the game. They come out of this process more interesting, though heavily right-handed (first baseman Yonder Alonso projects as the only lefty in the regular lineup) and suspect defensively, specifically with Myers being shifted to center field.

Still, the Padres saw Shields as a gift they could not reject. Here is why:

1. San Diego arguably has been the most aggressive team trying to trade for the Phillies’ Cole Hamels, who is younger and better than Shields. However, Hamels has at least four years and $96 million left on his contract, and it could go higher depending if he triggers vesting option provisions. Plus, to get Hamels, San Diego would have to give up elite prospects. All of their wheeling-and-dealing has depleted the system, yet the Padres did not surrender any of their three prized farmhands: catcher Austin Hedges, pitcher Matt Wisler and outfielder Hunter Renfroe.

However, because the Royals put the qualifying offer on Shields, San Diego would lose the 13th pick in the draft by signing of Shields.

2. Shields has been a key performer and clubhouse leader for two small-market clubs who have finished over .500 in each of the last seven seasons. San Diego has had a losing record in the past four years, but has gone in heavily to reverse that in 2015. Shields’ experience helping teams in just this situation could prove valuable.

3. Think of the Padres as the team that has spent so much on a poker hand they feel they can’t fold now and just might have to raise. Shields is the raise, a further attempt to push from also-ran status to surer contention in a year that now would be borderline devastating if the Padres do not make the playoffs.

4. Shields is a hedge in the near future with Ian Kennedy scheduled to become a free agent after the 2015 season and Andrew Cashner after the 2016 season.

5. It takes financial resolve now to include Shields’ contract, but Upton ($14.5 million in 2015), Kennedy ($9.85 million), Joaquin Benoit ($8 million with a $7.5 million option for 2016 or $1 million buyout), Carlos Quentin ($8 million with a $10 million option for 2016 or $3 million buyout), Cory Luebke ($5.25 million with a $1.75 million buyout on a ’16 option), Wil Venable ($4.25 million), Shawn Kelley, Brandon Morrow and Josh Johnson all can be free agents after this season. Cashner and Cameron Maybin can be free agents after 2016.

Even with Kemp going from costing the Padres $3 million this year to $18 million from 2016-19, San Diego has a ton of money coming off its books the next two years.