namespace ZB.MOM.WW.Secrets.Tests.Fakes;

/// <summary>
/// A <see cref="TimeProvider"/> test double with a settable <see cref="GetUtcNow"/>, so a test can
/// advance the clock past a cache TTL deterministically without sleeping.
/// </summary>
public sealed class MutableTimeProvider : TimeProvider
{
    private DateTimeOffset _utcNow;

    /// <summary>Creates a provider starting at the given instant (defaults to the Unix epoch).</summary>
    /// <param name="start">The initial <see cref="GetUtcNow"/> value.</param>
    public MutableTimeProvider(DateTimeOffset? start = null) =>
        _utcNow = start ?? DateTimeOffset.UnixEpoch;

    /// <summary>Gets or sets the current UTC instant this provider reports.</summary>
    public DateTimeOffset UtcNow
    {
        get => _utcNow;
        set => _utcNow = value;
    }

    /// <summary>Moves the clock forward by <paramref name="delta"/>.</summary>
    /// <param name="delta">The amount to advance.</param>
    public void Advance(TimeSpan delta) => _utcNow += delta;

    /// <inheritdoc />
    public override DateTimeOffset GetUtcNow() => _utcNow;
}
