namespace NATS.Server.JetStream.Storage;

public sealed class StoredMessage
{
    public ulong Sequence { get; init; }
    public string Subject { get; init; } = string.Empty;
    public ReadOnlyMemory<byte> Payload { get; init; }
    internal ReadOnlyMemory<byte> RawHeaders { get; init; }
    public DateTime TimestampUtc { get; init; } = DateTime.UtcNow;
    public string? Account { get; init; }
    public bool Redelivered { get; init; }

    /// <summary>
    /// Optional message headers. Used for deduplication (Nats-Msg-Id) and source tracking.
    /// </summary>
    public IReadOnlyDictionary<string, string>? Headers { get; init; }

    /// <summary>
    /// Convenience accessor for the Nats-Msg-Id header value, used by source deduplication.
    /// </summary>
    public string? MsgId => Headers is not null && Headers.TryGetValue("Nats-Msg-Id", out var id) ? id : null;

    internal StoredMessageIndex ToIndex()
        => new(Sequence, Subject, Payload.Length, TimestampUtc);
}
