namespace MarketAlly.GitCommitEditor.Models; public sealed class ImproverState { private const int DefaultMaxHistorySize = 1000; private const int DefaultMaxHistoryAgeDays = 90; public List Repos { get; set; } = []; public List History { get; set; } = []; public Dictionary LastAnalyzedCommits { get; set; } = []; public DateTimeOffset LastUpdated { get; set; } = DateTimeOffset.UtcNow; /// /// Prunes history to keep only recent entries within size and age limits. /// /// Maximum number of history entries to retain. /// Maximum age in days for history entries. /// Number of entries removed. public int PruneHistory(int maxSize = DefaultMaxHistorySize, int maxAgeDays = DefaultMaxHistoryAgeDays) { var initialCount = History.Count; var cutoffDate = DateTimeOffset.UtcNow.AddDays(-maxAgeDays); // Remove entries older than max age History.RemoveAll(h => h.CreatedAt < cutoffDate); // If still over size limit, keep only the most recent entries if (History.Count > maxSize) { var sorted = History.OrderByDescending(h => h.CreatedAt).ToList(); History.Clear(); History.AddRange(sorted.Take(maxSize)); } return initialCount - History.Count; } /// /// Removes history entries for repositories that are no longer registered. /// /// Number of orphaned entries removed. public int RemoveOrphanedHistory() { var repoIds = Repos.Select(r => r.Id).ToHashSet(); var initialCount = History.Count; History.RemoveAll(h => !repoIds.Contains(h.RepoId)); return initialCount - History.Count; } }