using System.ComponentModel.DataAnnotations; using InsightsService.Clients; using InsightsService.Domain; namespace InsightsService.Application; public sealed class InsightsApplicationService(ObservationServiceClient observations, InsightCalculator calculator) { public async Task GetSummaryAsync(Guid patientId, DateTime from, DateTime to, ContextSelection selection, CancellationToken cancellationToken) { from = NormalizeTimestamp(from); to = NormalizeTimestamp(to); if (to < from) throw new ArgumentException("The end time must not be earlier than the start time.", nameof(to)); Validator.ValidateObject(selection, new ValidationContext(selection), validateAllProperties: true); var history = await observations.GetByPeriodAsync(patientId, from, to, selection, cancellationToken); return calculator.CalculateSummary(patientId, from, to, history, selection); } private static DateTime NormalizeTimestamp(DateTime value) => DateTime.SpecifyKind(value.Kind == DateTimeKind.Unspecified ? value : value.ToUniversalTime(), DateTimeKind.Unspecified); }