public static class TrendContextBuilder
{
    public static TrendContext? BuildContext(
        string observationCode,
        IReadOnlyList<TrendHistoryEntry> history,
        decimal currentValue)
    {
        if (history.Count < 2) return null;

        var oldest = history.First();
        var newest = history.Last();
        var duration = newest.RecordedAt - oldest.RecordedAt;
        if (duration <= TimeSpan.Zero || oldest.Value == 0) return null;

        var pctChange = (double)((newest.Value - oldest.Value) / oldest.Value * 100m);
        var direction = pctChange >= 0 ? "Increasing" : "Decreasing";

        return new TrendContext
        {
            Parameter = TrendCalculator.DisplayName(observationCode),
            PercentChange = Math.Round(Math.Abs(pctChange), 1),
            Duration = duration,
            Direction = direction
        };
    }
}