| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | |
| | | 4 | | namespace RoleRadar.Domain.Tracking; |
| | | 5 | | |
| | | 6 | | public enum DateResolutionKind |
| | | 7 | | { |
| | | 8 | | Exact, |
| | | 9 | | InferredSameYear, |
| | | 10 | | InferredRollback, |
| | | 11 | | DefaultedBlank, |
| | | 12 | | DefaultedImpossible |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | public sealed record ResolvedDateOutcome( |
| | | 16 | | DateOnly ResolvedDate, |
| | | 17 | | DateResolutionKind Kind, |
| | | 18 | | bool IsApproximate, |
| | | 19 | | bool IsFlagged, |
| | | 20 | | string? Note); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Domain rules for parsing and inferring dates from legacy tracker inputs, |
| | | 24 | | /// supporting month-day derivation with calendar rollback, approximation markers, |
| | | 25 | | /// stated-date parentheticals, and graceful defaulting of impossible dates. |
| | | 26 | | /// </summary> |
| | | 27 | | public static partial class DateInferenceRule |
| | | 28 | | { |
| | | 29 | | [GeneratedRegex(@"\(([^)]+)\)")] |
| | | 30 | | private static partial Regex ParentheticalRegex(); |
| | | 31 | | |
| | | 32 | | [GeneratedRegex(@"^(?<month>\d{1,2})[/\-](?<day>\d{1,2})$")] |
| | | 33 | | private static partial Regex MonthDayRegex(); |
| | | 34 | | |
| | | 35 | | public static ResolvedDateOutcome Resolve(string? rawInput, DateOnly defaultDate) |
| | | 36 | | { |
| | 334 | 37 | | if (string.IsNullOrWhiteSpace(rawInput)) |
| | | 38 | | { |
| | 68 | 39 | | return new ResolvedDateOutcome( |
| | 68 | 40 | | ResolvedDate: defaultDate, |
| | 68 | 41 | | Kind: DateResolutionKind.DefaultedBlank, |
| | 68 | 42 | | IsApproximate: false, |
| | 68 | 43 | | IsFlagged: false, |
| | 68 | 44 | | Note: null); |
| | | 45 | | } |
| | | 46 | | |
| | 266 | 47 | | var text = rawInput.Trim(); |
| | 266 | 48 | | var isApproximate = false; |
| | 266 | 49 | | string? note = null; |
| | | 50 | | |
| | | 51 | | // Check for leading approximation marker "~" |
| | 266 | 52 | | if (text.StartsWith('~')) |
| | | 53 | | { |
| | 8 | 54 | | isApproximate = true; |
| | 8 | 55 | | text = text[1..].Trim(); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Check for parenthetical e.g. "2026-10-15 (stated date)" or entirely parenthesized "(10/15)" |
| | 266 | 59 | | var match = ParentheticalRegex().Match(text); |
| | 266 | 60 | | if (match.Success) |
| | | 61 | | { |
| | 8 | 62 | | var inner = match.Groups[1].Value.Trim(); |
| | 8 | 63 | | var remaining = text.Remove(match.Index, match.Length).Trim(); |
| | 8 | 64 | | if (string.IsNullOrWhiteSpace(remaining)) |
| | | 65 | | { |
| | | 66 | | // The entire date was wrapped in parentheses e.g. "(10/15)" |
| | 6 | 67 | | text = inner; |
| | 6 | 68 | | isApproximate = true; |
| | 6 | 69 | | note = "stated date in parentheses"; |
| | | 70 | | } |
| | | 71 | | else |
| | | 72 | | { |
| | 2 | 73 | | note = inner; |
| | 2 | 74 | | text = remaining; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // 1. Try full ISO date YYYY-MM-DD |
| | 266 | 79 | | if (DateOnly.TryParseExact(text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var fullDa |
| | | 80 | | { |
| | 200 | 81 | | return new ResolvedDateOutcome( |
| | 200 | 82 | | ResolvedDate: fullDate, |
| | 200 | 83 | | Kind: DateResolutionKind.Exact, |
| | 200 | 84 | | IsApproximate: isApproximate, |
| | 200 | 85 | | IsFlagged: isApproximate || note is not null, |
| | 200 | 86 | | Note: note); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | // 2. Try month-day partial format M/D, MM/DD, M-D, MM-DD |
| | 66 | 90 | | var mdMatch = MonthDayRegex().Match(text); |
| | 66 | 91 | | if (mdMatch.Success) |
| | | 92 | | { |
| | 64 | 93 | | var month = int.Parse(mdMatch.Groups["month"].Value, CultureInfo.InvariantCulture); |
| | 64 | 94 | | var day = int.Parse(mdMatch.Groups["day"].Value, CultureInfo.InvariantCulture); |
| | | 95 | | |
| | 64 | 96 | | if (month is < 1 or > 12) |
| | | 97 | | { |
| | 2 | 98 | | return new ResolvedDateOutcome( |
| | 2 | 99 | | ResolvedDate: defaultDate, |
| | 2 | 100 | | Kind: DateResolutionKind.DefaultedImpossible, |
| | 2 | 101 | | IsApproximate: isApproximate, |
| | 2 | 102 | | IsFlagged: true, |
| | 2 | 103 | | Note: note); |
| | | 104 | | } |
| | | 105 | | |
| | 62 | 106 | | var daysInTargetMonth = DateTime.DaysInMonth(defaultDate.Year, month); |
| | 62 | 107 | | if (day < 1 || day > daysInTargetMonth) |
| | | 108 | | { |
| | 6 | 109 | | return new ResolvedDateOutcome( |
| | 6 | 110 | | ResolvedDate: defaultDate, |
| | 6 | 111 | | Kind: DateResolutionKind.DefaultedImpossible, |
| | 6 | 112 | | IsApproximate: isApproximate, |
| | 6 | 113 | | IsFlagged: true, |
| | 6 | 114 | | Note: note); |
| | | 115 | | } |
| | | 116 | | |
| | 56 | 117 | | var candidate = new DateOnly(defaultDate.Year, month, day); |
| | 56 | 118 | | if (candidate > defaultDate) |
| | | 119 | | { |
| | | 120 | | // Rollback rule: derives date in previous calendar year |
| | 2 | 121 | | var rollbackYear = defaultDate.Year - 1; |
| | 2 | 122 | | var daysInRollbackMonth = DateTime.DaysInMonth(rollbackYear, month); |
| | 2 | 123 | | var safeDay = Math.Min(day, daysInRollbackMonth); |
| | 2 | 124 | | var rollbackDate = new DateOnly(rollbackYear, month, safeDay); |
| | | 125 | | |
| | 2 | 126 | | return new ResolvedDateOutcome( |
| | 2 | 127 | | ResolvedDate: rollbackDate, |
| | 2 | 128 | | Kind: DateResolutionKind.InferredRollback, |
| | 2 | 129 | | IsApproximate: isApproximate, |
| | 2 | 130 | | IsFlagged: isApproximate || note is not null, |
| | 2 | 131 | | Note: note); |
| | | 132 | | } |
| | | 133 | | |
| | 54 | 134 | | return new ResolvedDateOutcome( |
| | 54 | 135 | | ResolvedDate: candidate, |
| | 54 | 136 | | Kind: DateResolutionKind.InferredSameYear, |
| | 54 | 137 | | IsApproximate: isApproximate, |
| | 54 | 138 | | IsFlagged: isApproximate || note is not null, |
| | 54 | 139 | | Note: note); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // Any unrecognized or malformed date format throws ArgumentException for caller validation handling |
| | 2 | 143 | | throw new ArgumentException($"Malformed date: '{rawInput}'. Expected ISO 'YYYY-MM-DD' or month-day 'M/D'.", name |
| | | 144 | | } |
| | | 145 | | } |