using System.Collections.Concurrent;
namespace Campaign_Tracker.Server.LegacyData.Schema;
///
/// Stores recent compatibility-check results so administrators can view a
/// history of runs (Story 1.7 AC #5). The default implementation is in-process
/// and bounded; production deployments can swap a durable store via DI.
///
public interface ILegacySchemaCheckHistory
{
void Record(LegacySchemaCheckResult result);
IReadOnlyList GetRecent(int maxCount = 50);
}
public sealed class InMemoryLegacySchemaCheckHistory : ILegacySchemaCheckHistory
{
private const int Capacity = 200;
private readonly ConcurrentQueue _results = new();
private readonly object _trimLock = new();
public void Record(LegacySchemaCheckResult result)
{
ArgumentNullException.ThrowIfNull(result);
_results.Enqueue(result);
Trim();
}
public IReadOnlyList GetRecent(int maxCount = 50)
{
if (maxCount <= 0) return [];
return _results.Reverse().Take(maxCount).ToArray();
}
private void Trim()
{
if (_results.Count <= Capacity) return;
lock (_trimLock)
{
while (_results.Count > Capacity && _results.TryDequeue(out _)) { }
}
}
}