namespace NATS.Server.JetStream.Consumers;

/// <summary>
/// A pull request with mutable batch and byte tracking for delivery fulfillment.
/// Go reference: consumer.go waitingRequest / processNextMsgRequest.
/// </summary>
public sealed record PullRequest(
    string ReplyTo,
    int Batch,
    long MaxBytes,
    DateTimeOffset Expires,
    bool NoWait,
    string? PinId = null)
{
    /// <summary>Remaining messages allowed for this request.</summary>
    public int RemainingBatch { get; private set; } = Batch;

    /// <summary>Remaining bytes allowed for this request (only meaningful when MaxBytes > 0).</summary>
    public long RemainingBytes { get; private set; } = MaxBytes;

    /// <summary>True when batch or bytes (if set) are exhausted.</summary>
    public bool IsExhausted => RemainingBatch <= 0 || (MaxBytes > 0 && RemainingBytes <= 0);

    /// <summary>Decrement remaining batch count by one.</summary>
    public void ConsumeBatch() => RemainingBatch--;

    /// <summary>Subtract delivered bytes from remaining byte budget.</summary>
    public void ConsumeBytes(long bytes) => RemainingBytes -= bytes;
}

/// <summary>
/// FIFO queue of pull requests with expiry support.
/// Unlike PullRequestWaitQueue (priority-based), this is a simple FIFO with
/// RemoveExpired cleanup and mutable request tracking.
/// Go reference: consumer.go waitQueue / processNextMsgRequest.
/// </summary>
public sealed class WaitingRequestQueue
{
    private readonly LinkedList<PullRequest> _queue = new();

    public int Count => _queue.Count;
    public bool IsEmpty => _queue.Count == 0;

    public void Enqueue(PullRequest request) => _queue.AddLast(request);

    public PullRequest? TryDequeue()
    {
        if (_queue.Count == 0) return null;
        var first = _queue.First!.Value;
        _queue.RemoveFirst();
        return first;
    }

    public void RemoveExpired(DateTimeOffset now)
    {
        var node = _queue.First;
        while (node != null)
        {
            var next = node.Next;
            if (node.Value.Expires <= now)
                _queue.Remove(node);
            node = next;
        }
    }
}
