namespace NATS.Server;

/// <summary>
/// Connection state flags tracked per client.
/// Corresponds to Go server/client.go clientFlag bitfield.
/// Thread-safe via Interlocked operations on the backing int.
/// </summary>
[Flags]
public enum ClientFlags
{
    ConnectReceived = 1 << 0,
    FirstPongSent = 1 << 1,
    HandshakeComplete = 1 << 2,
    CloseConnection = 1 << 3,
    WriteLoopStarted = 1 << 4,
    IsSlowConsumer = 1 << 5,
    ConnectProcessFinished = 1 << 6,
    TraceMode = 1 << 7,
}

/// <summary>
/// Thread-safe holder for client flags using Interlocked operations.
/// </summary>
public sealed class ClientFlagHolder
{
    private int _flags;

    public void SetFlag(ClientFlags flag)
    {
        Interlocked.Or(ref _flags, (int)flag);
    }

    public void ClearFlag(ClientFlags flag)
    {
        Interlocked.And(ref _flags, ~(int)flag);
    }

    public bool HasFlag(ClientFlags flag)
    {
        return (Volatile.Read(ref _flags) & (int)flag) != 0;
    }
}
