Walgreens Boots Alliance (WBA -0.58%) operates neighborhood retail pharmacies across the country. It has played an important role in helping millions of people get vaccinated, and the company is expanding its operations, diving deeper into healthcare. It also provides investors with a solid dividend, one that has been growing over the years.

But despite all these positives, the stock has been struggling. It's down more than 40% in five years while investors who took the safe approach and invested in the S&P 500 would have been up around 50% over the same stretch. Is this an underdog stock that could outperform in the future, or is Walgreens too risky of a buy, even at its drastically reduced valuation?

The company's fundamentals are underwhelming

A good place to start when assessing a company's business is by looking at its gross margin to see how much of revenue is left after cost of goods sold. For Walgreens, it doesn't paint a pretty picture.

WBA Gross Profit Margin (Quarterly) Chart

WBA Gross Profit Margin (Quarterly) data by YCharts

Ignoring the volatility surrounding the early stages of the COVID-19 pandemic, the overarching trend has been that the company's gross margin has been down over the past decade. Now at around 20%, that doesn't leave a whole lot left to cover operating expenses. For its fiscal year ended Aug. 31, Walgreens reported a profit of $4.3 billion on sales of $132.7 billion, for a profit margin of just 3.3%.

That's concerning, especially with inflation still problematic and the economy heading into a potential recession next year. That could put further strain on Walgreens' bottom line. For the last three months of the fiscal year, the company incurred a net loss of $415 million. 

Walgreens' expansion into healthcare could be a risky one

What will likely be the determining factor in whether Walgreens can outperform the broader market going forward will be the success of its healthcare strategy.

Last year, Walgreens invested $5.2 billion into primary care operator VillageMD. This year, it has added another $3.5 billion to help fund the company's acquisition of Summit Health-City MD, another primary care business. Walgreens is investing big money in the hopes that primary care will pay off for its new healthcare business and that by 2025 the segment could bring in up to $16 billion in revenue.

But at the same time, the company isn't expecting it to generate meaningful profits even by fiscal 2024, when it expects that the segment will only be slightly accretive to its bottom line, and that's on an adjusted earnings basis. That suggests that profitability in the segment could still be multiple years away; in Walgreens' most recent quarter, its U.S. healthcare business incurred an operating loss of $338 million and even its gross profit was a negative $37 million.

While Walgreen is banking on the segment to open hundreds of primary care practices at its retail locations, it may not help the company's bottom line at a time when it might desperately need assistance.

Is Walgreens stock a buy?

One of the most attractive reasons to invest in Walgreens is for its dividend yield, which at 4.7% is close to three times the S&P 500 average of 1.7%. It is also a Dividend Aristocrat and has raised its payouts for decades. 

However, that can all be moot if the business itself is struggling. Walgreens is in a position where it needs to do something to attract investors and reenergize its business, and getting deeper into healthcare appears to be a natural extension of its existing operations. There is undoubtedly some risk with the billions it is investing in healthcare today, and for that reason, it may not be suitable for conservative, risk-averse investors.

But given that the stock trades at only eight times earnings, there's a solid margin of safety here for investors so that even if the business struggles next year, it may not be a catastrophic decline for Walgreens given all the pessimism that is already priced in; the stock was trading near its 10-year lows just a few months ago. If you're comfortable with the risk, there could be significant upside for the stock if its healthcare strategy pays off.